@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 97 lines 3.29 KB
Raw
1 import { describe, expect, it } from "@effect/vitest";
2 import * as Effect from "effect/Effect";
3 import { vi } from "vite-plus/test";
4
5 vi.mock("react-native", () => ({
6 Platform: { OS: "ios" },
7 }));
8
9 vi.mock("expo-secure-store", () => ({
10 deleteItemAsync: vi.fn(),
11 getItemAsync: vi.fn(),
12 setItemAsync: vi.fn(),
13 }));
14
15 import { CONNECTION_CATALOG_KEY, LEGACY_CONNECTIONS_KEY, make } from "./catalog-store";
16 import * as MobileSecureStorage from "../persistence/mobile-secure-storage";
17
18 function makeStorage(initial: Readonly<Record<string, string>>) {
19 const values = new Map(Object.entries(initial));
20 const deleted: Array<string> = [];
21 const storage = MobileSecureStorage.MobileSecureStorage.of({
22 getItem: (key) => Effect.sync(() => values.get(key) ?? null),
23 setItem: (key, value) =>
24 Effect.sync(() => {
25 values.set(key, value);
26 }),
27 removeItem: (key) =>
28 Effect.sync(() => {
29 deleted.push(key);
30 values.delete(key);
31 }),
32 });
33 return { deleted, storage, values };
34 }
35
36 describe("mobile connection catalog storage", () => {
37 it.effect("recovers from a corrupt current catalog", () =>
38 Effect.gen(function* () {
39 const memory = makeStorage({
40 [CONNECTION_CATALOG_KEY]: "{not-json",
41 });
42 const catalog = yield* make().pipe(
43 Effect.provideService(MobileSecureStorage.MobileSecureStorage, memory.storage),
44 );
45
46 expect((yield* catalog.read).targets).toEqual([]);
47 expect(memory.deleted).toEqual([CONNECTION_CATALOG_KEY]);
48 }),
49 );
50
51 it.effect("replaces and removes a corrupt legacy catalog", () =>
52 Effect.gen(function* () {
53 const memory = makeStorage({
54 [LEGACY_CONNECTIONS_KEY]: JSON.stringify({ connections: [{ invalid: true }] }),
55 });
56 const catalog = yield* make().pipe(
57 Effect.provideService(MobileSecureStorage.MobileSecureStorage, memory.storage),
58 );
59
60 expect((yield* catalog.read).targets).toEqual([]);
61 expect(memory.deleted).toEqual([LEGACY_CONNECTIONS_KEY]);
62 expect(memory.values.has(CONNECTION_CATALOG_KEY)).toBe(true);
63 }),
64 );
65
66 it.effect("falls back to valid legacy data when the current catalog is corrupt", () =>
67 Effect.gen(function* () {
68 const memory = makeStorage({
69 [CONNECTION_CATALOG_KEY]: "{not-json",
70 [LEGACY_CONNECTIONS_KEY]: JSON.stringify({
71 connections: [
72 {
73 environmentId: "legacy-environment",
74 environmentLabel: "Legacy",
75 pairingUrl: "https://legacy.example.test/pair",
76 displayUrl: "https://legacy.example.test",
77 httpBaseUrl: "https://legacy.example.test",
78 wsBaseUrl: "wss://legacy.example.test",
79 bearerToken: "legacy-token",
80 authenticationMethod: "bearer",
81 },
82 ],
83 }),
84 });
85 const catalog = yield* make().pipe(
86 Effect.provideService(MobileSecureStorage.MobileSecureStorage, memory.storage),
87 );
88
89 expect((yield* catalog.read).targets).toHaveLength(1);
90 expect(memory.deleted).toEqual([CONNECTION_CATALOG_KEY, LEGACY_CONNECTIONS_KEY]);
91
92 yield* catalog.update((document) => document);
93 expect(memory.values.has(CONNECTION_CATALOG_KEY)).toBe(true);
94 expect(memory.values.has(LEGACY_CONNECTIONS_KEY)).toBe(false);
95 }),
96 );
97 });