diff --git a/tests/frontend/storage-path-favorite-records.test.mjs b/tests/frontend/storage-path-favorite-records.test.mjs new file mode 100644 index 00000000..dbd13a2c --- /dev/null +++ b/tests/frontend/storage-path-favorite-records.test.mjs @@ -0,0 +1,211 @@ +import test from "node:test"; +import assert from "node:assert/strict"; + +import { + listFavoriteStoragePathRecords, +} from "../../src/lib/storage-path-history.js"; + +const FAVORITES_KEY = "droboticflow:storage-path-favorites:v1"; + +function createMemoryStorage(seed = {}) { + const values = new Map(Object.entries(seed)); + return { + getItem(key) { + return values.has(key) ? values.get(key) : null; + }, + setItem(key, value) { + values.set(key, String(value)); + }, + }; +} + +function storageWithFavorites(raw) { + return createMemoryStorage({ [FAVORITES_KEY]: raw }); +} + +test("listFavoriteStoragePathRecords returns an explicit label unchanged", () => { + const storage = storageWithFavorites( + JSON.stringify([ + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "algo/demo", + label: "My pinned folder", + pathKey: "datastorage/algo/demo", + }, + { + scopeKey: "connection:conn-1", + bucket: "archive", + prefix: "", + label: "Archive root", + pathKey: "archive", + }, + ]), + ); + + assert.deepEqual(listFavoriteStoragePathRecords(storage), [ + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "algo/demo", + label: "My pinned folder", + }, + { + scopeKey: "connection:conn-1", + bucket: "archive", + prefix: "", + label: "Archive root", + }, + ]); +}); + +test("listFavoriteStoragePathRecords falls back to bucket/prefix or bucket when label is missing", () => { + const storage = storageWithFavorites( + JSON.stringify([ + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "algo/demo", + pathKey: "datastorage/algo/demo", + }, + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "processed", + label: "", + pathKey: "datastorage/processed", + }, + { + scopeKey: "source:bos://archive", + bucket: "archive", + pathKey: "archive", + }, + { + scopeKey: "source:bos://archive", + bucket: "media", + prefix: "", + label: "", + pathKey: "media", + }, + ]), + ); + + assert.deepEqual(listFavoriteStoragePathRecords(storage), [ + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "algo/demo", + label: "datastorage/algo/demo", + }, + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "processed", + label: "datastorage/processed", + }, + { + scopeKey: "source:bos://archive", + bucket: "archive", + prefix: "", + label: "archive", + }, + { + scopeKey: "source:bos://archive", + bucket: "media", + prefix: "", + label: "media", + }, + ]); +}); + +test("listFavoriteStoragePathRecords returns an empty list for empty storage", () => { + assert.deepEqual(listFavoriteStoragePathRecords(createMemoryStorage()), []); + assert.deepEqual( + listFavoriteStoragePathRecords(storageWithFavorites("")), + [], + ); + assert.deepEqual(listFavoriteStoragePathRecords(null), []); +}); + +test("listFavoriteStoragePathRecords returns an empty list for non-array payloads", () => { + for (const raw of [ + JSON.stringify({ scopeKey: "connection:conn-1", bucket: "datastorage" }), + JSON.stringify("datastorage"), + JSON.stringify(42), + "null", + ]) { + assert.deepEqual( + listFavoriteStoragePathRecords(storageWithFavorites(raw)), + [], + `payload ${raw} should yield an empty list`, + ); + } +}); + +test("listFavoriteStoragePathRecords returns an empty list for invalid JSON", () => { + assert.deepEqual( + listFavoriteStoragePathRecords(storageWithFavorites("{bad-json")), + [], + ); + assert.deepEqual( + listFavoriteStoragePathRecords(storageWithFavorites("[{")), + [], + ); +}); + +test("listFavoriteStoragePathRecords ignores records missing scopeKey, bucket or pathKey", () => { + const storage = storageWithFavorites( + JSON.stringify([ + { + bucket: "datastorage", + prefix: "no-scope", + pathKey: "datastorage/no-scope", + }, + { + scopeKey: " ", + bucket: "datastorage", + prefix: "blank-scope", + pathKey: "datastorage/blank-scope", + }, + { + scopeKey: "connection:conn-1", + prefix: "no-bucket", + pathKey: "no-bucket", + }, + { + scopeKey: "connection:conn-1", + bucket: "", + prefix: "empty-bucket", + pathKey: "empty-bucket", + }, + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "no-path-key", + }, + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "empty-path-key", + pathKey: "", + }, + null, + "datastorage", + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "kept", + pathKey: "datastorage/kept", + }, + ]), + ); + + assert.deepEqual(listFavoriteStoragePathRecords(storage), [ + { + scopeKey: "connection:conn-1", + bucket: "datastorage", + prefix: "kept", + label: "datastorage/kept", + }, + ]); +});