Open History to see the last commit
1import { EnvironmentId, type PersistedSavedEnvironmentRecord } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3
4const testEnvironmentId = EnvironmentId.make("environment-1");
5
6const savedRegistryRecord: PersistedSavedEnvironmentRecord = {
7 environmentId: testEnvironmentId,
8 label: "Remote environment",
9 httpBaseUrl: "https://remote.example.com/",
10 wsBaseUrl: "wss://remote.example.com/",
11 createdAt: "2026-04-09T00:00:00.000Z",
12 lastConnectedAt: null,
13 desktopSsh: {
14 alias: "devbox",
15 hostname: "devbox.example.com",
16 username: "julius",
17 port: 22,
18 },
19};
20
21function createLocalStorageStub(): Storage {
22 const store = new Map<string, string>();
23 return {
24 getItem: (key) => store.get(key) ?? null,
25 setItem: (key, value) => {
26 store.set(key, value);
27 },
28 removeItem: (key) => {
29 store.delete(key);
30 },
31 clear: () => {
32 store.clear();
33 },
34 key: (index) => [...store.keys()][index] ?? null,
35 get length() {
36 return store.size;
37 },
38 };
39}
40
41function getTestWindow(): Window & typeof globalThis {
42 const localStorage = createLocalStorageStub();
43 const testWindow = {
44 localStorage,
45 } as Window & typeof globalThis;
46 vi.stubGlobal("window", testWindow);
47 vi.stubGlobal("localStorage", localStorage);
48 return testWindow;
49}
50
51afterEach(() => {
52 vi.resetModules();
53 vi.unstubAllGlobals();
54 vi.restoreAllMocks();
55});
56
57describe("clientPersistenceStorage", () => {
58 it("stores browser secrets inline with the saved environment record", async () => {
59 const testWindow = getTestWindow();
60 const {
61 SAVED_ENVIRONMENT_REGISTRY_STORAGE_KEY,
62 readBrowserSavedEnvironmentRegistry,
63 readBrowserSavedEnvironmentSecret,
64 writeBrowserSavedEnvironmentRegistry,
65 writeBrowserSavedEnvironmentSecret,
66 } = await import("./clientPersistenceStorage");
67
68 writeBrowserSavedEnvironmentRegistry([savedRegistryRecord]);
69 expect(writeBrowserSavedEnvironmentSecret(testEnvironmentId, "bearer-token")).toBe(true);
70 writeBrowserSavedEnvironmentRegistry([savedRegistryRecord]);
71
72 expect(readBrowserSavedEnvironmentRegistry()).toEqual([savedRegistryRecord]);
73 expect(readBrowserSavedEnvironmentSecret(testEnvironmentId)).toBe("bearer-token");
74 expect(
75 JSON.parse(testWindow.localStorage.getItem(SAVED_ENVIRONMENT_REGISTRY_STORAGE_KEY)!),
76 ).toEqual({
77 version: 1,
78 records: [
79 {
80 ...savedRegistryRecord,
81 bearerToken: "bearer-token",
82 },
83 ],
84 });
85 });
86});