Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3
4import {
5 CONNECTION_CATALOG_KEY,
6 LEGACY_CONNECTIONS_KEY,
7 makeCatalogStore,
8 type SecureCatalogStorage,
9} from "./catalog-store";
10
11function makeStorage(initial: Readonly<Record<string, string>>) {
12 const values = new Map(Object.entries(initial));
13 const deleted: Array<string> = [];
14 const storage: SecureCatalogStorage = {
15 getItem: (key) => Effect.sync(() => values.get(key) ?? null),
16 setItem: (key, value) =>
17 Effect.sync(() => {
18 values.set(key, value);
19 }),
20 deleteItem: (key) =>
21 Effect.sync(() => {
22 deleted.push(key);
23 values.delete(key);
24 }),
25 };
26 return { deleted, storage, values };
27}
28
29describe("mobile connection catalog storage", () => {
30 it.effect("recovers from a corrupt current catalog", () =>
31 Effect.gen(function* () {
32 const memory = makeStorage({
33 [CONNECTION_CATALOG_KEY]: "{not-json",
34 });
35 const catalog = yield* makeCatalogStore(memory.storage);
36
37 expect((yield* catalog.read).targets).toEqual([]);
38 expect(memory.deleted).toEqual([CONNECTION_CATALOG_KEY]);
39 }),
40 );
41
42 it.effect("replaces and removes a corrupt legacy catalog", () =>
43 Effect.gen(function* () {
44 const memory = makeStorage({
45 [LEGACY_CONNECTIONS_KEY]: JSON.stringify({ connections: [{ invalid: true }] }),
46 });
47 const catalog = yield* makeCatalogStore(memory.storage);
48
49 expect((yield* catalog.read).targets).toEqual([]);
50 expect(memory.deleted).toEqual([LEGACY_CONNECTIONS_KEY]);
51 expect(memory.values.has(CONNECTION_CATALOG_KEY)).toBe(true);
52 }),
53 );
54
55 it.effect("falls back to valid legacy data when the current catalog is corrupt", () =>
56 Effect.gen(function* () {
57 const memory = makeStorage({
58 [CONNECTION_CATALOG_KEY]: "{not-json",
59 [LEGACY_CONNECTIONS_KEY]: JSON.stringify({
60 connections: [
61 {
62 environmentId: "legacy-environment",
63 environmentLabel: "Legacy",
64 pairingUrl: "https://legacy.example.test/pair",
65 displayUrl: "https://legacy.example.test",
66 httpBaseUrl: "https://legacy.example.test",
67 wsBaseUrl: "wss://legacy.example.test",
68 bearerToken: "legacy-token",
69 authenticationMethod: "bearer",
70 },
71 ],
72 }),
73 });
74 const catalog = yield* makeCatalogStore(memory.storage);
75
76 expect((yield* catalog.read).targets).toHaveLength(1);
77 expect(memory.deleted).toEqual([CONNECTION_CATALOG_KEY, LEGACY_CONNECTIONS_KEY]);
78
79 yield* catalog.update((document) => document);
80 expect(memory.values.has(CONNECTION_CATALOG_KEY)).toBe(true);
81 expect(memory.values.has(LEGACY_CONNECTIONS_KEY)).toBe(false);
82 }),
83 );
84});