g1t/apps/web/app/lib/agent-setup.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { |
| 5 | AGENTS, |
| 6 | AGENT_IDS, |
| 7 | AGENT_STORAGE_KEY, |
| 8 | DEFAULT_AGENT, |
| 9 | MCP_URL, |
| 10 | cursorInstallLink, |
| 11 | getAgentChoice, |
| 12 | parseAgent, |
| 13 | readAgentChoice, |
| 14 | setAgentChoice, |
| 15 | subscribeAgentChoice, |
| 16 | writeAgentChoice, |
| 17 | } from "./agent-setup.ts"; |
| 18 | |
| 19 | function memory() { |
| 20 | const map = new Map<string, string>(); |
| 21 | return { |
| 22 | getItem: (key: string) => map.get(key) ?? null, |
| 23 | setItem: (key: string, value: string) => void map.set(key, value), |
| 24 | map, |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | const throwing = { |
| 29 | getItem: () => { |
| 30 | throw new Error("blocked"); |
| 31 | }, |
| 32 | setItem: () => { |
| 33 | throw new Error("blocked"); |
| 34 | }, |
| 35 | }; |
| 36 | |
| 37 | test("a choice is remembered and read back", () => { |
| 38 | const store = memory(); |
| 39 | assert.equal(writeAgentChoice(() => store, "codex"), true); |
| 40 | assert.equal(store.map.get(AGENT_STORAGE_KEY), "codex"); |
| 41 | assert.equal(readAgentChoice(() => store), "codex"); |
| 42 | }); |
| 43 | |
| 44 | test("no choice, an unknown one, or storage that throws falls back to the default", () => { |
| 45 | assert.equal(readAgentChoice(() => memory()), DEFAULT_AGENT); |
| 46 | const store = memory(); |
| 47 | store.setItem(AGENT_STORAGE_KEY, "notepad"); |
| 48 | assert.equal(readAgentChoice(() => store), DEFAULT_AGENT); |
| 49 | assert.equal(readAgentChoice(() => throwing), DEFAULT_AGENT); |
| 50 | assert.equal(readAgentChoice(() => null), DEFAULT_AGENT); |
| 51 | assert.equal( |
| 52 | readAgentChoice(() => { |
| 53 | throw new Error("no localStorage"); |
| 54 | }), |
| 55 | DEFAULT_AGENT, |
| 56 | ); |
| 57 | assert.equal(writeAgentChoice(() => throwing, "cursor"), false); |
| 58 | assert.equal(writeAgentChoice(() => null, "cursor"), false); |
| 59 | }); |
| 60 | |
| 61 | test("every toggle on the page follows the last choice", () => { |
| 62 | const store = memory(); |
| 63 | let heard = 0; |
| 64 | const stop = subscribeAgentChoice(() => heard++); |
| 65 | setAgentChoice("opencode", () => store); |
| 66 | assert.equal(getAgentChoice(), "opencode"); |
| 67 | assert.equal(store.map.get(AGENT_STORAGE_KEY), "opencode"); |
| 68 | assert.equal(heard, 1); |
| 69 | // Storage that refuses still changes the page. |
| 70 | setAgentChoice("cursor", () => throwing); |
| 71 | assert.equal(getAgentChoice(), "cursor"); |
| 72 | assert.equal(heard, 2); |
| 73 | stop(); |
| 74 | setAgentChoice("codex", () => store); |
| 75 | assert.equal(heard, 2); |
| 76 | }); |
| 77 | |
| 78 | test("parseAgent accepts only known agents", () => { |
| 79 | for (const id of AGENT_IDS) assert.equal(parseAgent(id), id); |
| 80 | assert.equal(parseAgent("Claude Code"), null); |
| 81 | assert.equal(parseAgent(null), null); |
| 82 | }); |
| 83 | |
| 84 | test("every snippet points at the MCP server", () => { |
| 85 | for (const id of AGENT_IDS) assert.ok(AGENTS[id].code.includes(MCP_URL), id); |
| 86 | assert.deepEqual(JSON.parse(AGENTS.cursor.code), { mcpServers: { g1t: { url: MCP_URL } } }); |
| 87 | assert.equal(JSON.parse(AGENTS.opencode.code).mcp.g1t.type, "remote"); |
| 88 | const config = new URL(cursorInstallLink()).searchParams.get("config"); |
| 89 | assert.deepEqual(JSON.parse(atob(config ?? "")), { url: MCP_URL }); |
| 90 | }); |