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, 03:37 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - launcher integration with filesystem and fetch. |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | import * as NodeOS from "node:os"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | import { expect, it, vi } from "vite-plus/test"; |
| 6 | import { flushTelemetry, observeSummaries, summarize } from "./telemetry.ts"; |
| 7 | |
| 8 | vi.mock("node:fs", async (importOriginal) => { |
| 9 | const fs = await importOriginal<typeof import("node:fs")>(); |
| 10 | return { ...fs, rmdirSync: vi.fn(fs.rmdirSync) }; |
| 11 | }); |
| 12 | |
| 13 | const task = (result: unknown) => ({ |
| 14 | package_name: "@team/server", |
| 15 | task_name: "typecheck", |
| 16 | command: "SECRET", |
| 17 | cwd: "/private", |
| 18 | result, |
| 19 | }); |
| 20 | |
| 21 | it("counts only confirmed hits, preserves their tier, and never transmits command text", () => { |
| 22 | expect( |
| 23 | summarize({ |
| 24 | tasks: [ |
| 25 | task({ CacheHit: { source: "Remote", saved_duration_ms: 1200 } }), |
| 26 | task({ CacheHit: { source: "Local", saved_duration_ms: 500 } }), |
| 27 | task({ RestoreFailed: {} }), |
| 28 | task({ Spawned: {} }), |
| 29 | task({ CacheHit: { saved_duration_ms: 999 } }), |
| 30 | task({ CacheHit: { source: "Remote", saved_duration_ms: -1 } }), |
| 31 | ], |
| 32 | }), |
| 33 | ).toEqual([ |
| 34 | { package: "@team/server", task: "typecheck", source: "remote", savedMs: 1200 }, |
| 35 | { package: "@team/server", task: "typecheck", source: "local", savedMs: 500 }, |
| 36 | { package: "@team/server", task: "typecheck", source: "failed", savedMs: 0 }, |
| 37 | { package: "@team/server", task: "typecheck", source: "executed", savedMs: 0 }, |
| 38 | { package: "@team/server", task: "typecheck", source: "unspecified", savedMs: 999 }, |
| 39 | ]); |
| 40 | }); |
| 41 | |
| 42 | it("deduplicates shared summaries and retries delivery without credentials in queued records", async () => { |
| 43 | const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-observer-")); |
| 44 | const home = NodePath.join(root, "home"); |
| 45 | const endpoint = "https://cache.example/projects/t3code"; |
| 46 | try { |
| 47 | const stopA = observeSummaries(root, endpoint, { T3_TASK_CACHE_HOST: "test-host" }, home); |
| 48 | const stopB = observeSummaries(root, endpoint, { T3_TASK_CACHE_HOST: "test-host" }, home); |
| 49 | const version = NodePath.join(root, "node_modules/.vite/task-cache/v18"); |
| 50 | NodeFS.mkdirSync(version); |
| 51 | NodeFS.writeFileSync( |
| 52 | NodePath.join(version, "last-summary.json"), |
| 53 | JSON.stringify({ |
| 54 | tasks: [task({ CacheHit: { source: "Remote", saved_duration_ms: 1200 } })], |
| 55 | }), |
| 56 | ); |
| 57 | stopA(); |
| 58 | stopB(); |
| 59 | const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry"); |
| 60 | const files = NodeFS.readdirSync(queue); |
| 61 | expect(files).toHaveLength(1); |
| 62 | const serialized = NodeFS.readFileSync(NodePath.join(queue, files[0]!), "utf8"); |
| 63 | expect(serialized).not.toContain("SECRET"); |
| 64 | expect(JSON.parse(serialized).event.host).toBe("test-host"); |
| 65 | NodeFS.mkdirSync(NodePath.join(home, ".config/t3-task-cache"), { recursive: true }); |
| 66 | NodeFS.writeFileSync( |
| 67 | NodePath.join(home, ".config/t3-task-cache/credentials.json"), |
| 68 | JSON.stringify({ [endpoint]: { name: "alice", token: "secret-token" } }), |
| 69 | ); |
| 70 | using request = vi |
| 71 | .spyOn(globalThis, "fetch") |
| 72 | .mockResolvedValueOnce(new Response(null, { status: 503 })) |
| 73 | .mockImplementationOnce(async () => { |
| 74 | const late = JSON.parse(serialized); |
| 75 | late.event.id = "b".repeat(64); |
| 76 | NodeFS.writeFileSync(NodePath.join(queue, "late.json"), JSON.stringify(late)); |
| 77 | return new Response(null, { status: 204 }); |
| 78 | }) |
| 79 | .mockResolvedValue(new Response(null, { status: 204 })); |
| 80 | await flushTelemetry(home, {}); |
| 81 | expect(NodeFS.readdirSync(queue)).toEqual(files); |
| 82 | await flushTelemetry(home, {}); |
| 83 | expect(NodeFS.readdirSync(queue)).toEqual([]); |
| 84 | expect(request).toHaveBeenCalledTimes(3); |
| 85 | expect(request.mock.calls[0]![0].toString()).toBe(`${endpoint}/telemetry`); |
| 86 | } finally { |
| 87 | NodeFS.rmSync(root, { recursive: true, force: true }); |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | it("skips unauthenticated endpoints without consuming the delivery budget", async () => { |
| 92 | const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-queue-")); |
| 93 | const endpoint = "https://cache.example/projects/t3code"; |
| 94 | try { |
| 95 | const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry"); |
| 96 | NodeFS.mkdirSync(queue, { recursive: true }); |
| 97 | const skipped = Array.from({ length: 101 }, (_, i) => `a${i}.json`); |
| 98 | for (const file of skipped) |
| 99 | NodeFS.writeFileSync( |
| 100 | NodePath.join(queue, file), |
| 101 | JSON.stringify({ endpoint: "https://old.example/projects/t3code", event: { id: file } }), |
| 102 | ); |
| 103 | NodeFS.writeFileSync( |
| 104 | NodePath.join(queue, "b.json"), |
| 105 | JSON.stringify({ endpoint, event: { id: "current" } }), |
| 106 | ); |
| 107 | NodeFS.mkdirSync(NodePath.join(home, ".config/t3-task-cache"), { recursive: true }); |
| 108 | NodeFS.writeFileSync( |
| 109 | NodePath.join(home, ".config/t3-task-cache/credentials.json"), |
| 110 | JSON.stringify({ [endpoint]: { name: "alice", token: "secret-token" } }), |
| 111 | ); |
| 112 | using request = vi |
| 113 | .spyOn(globalThis, "fetch") |
| 114 | .mockResolvedValue(new Response(null, { status: 204 })); |
| 115 | await flushTelemetry(home, {}); |
| 116 | expect(NodeFS.readdirSync(queue).sort()).toEqual(skipped.sort()); |
| 117 | expect(request).toHaveBeenCalledOnce(); |
| 118 | expect(request.mock.calls[0]![0].toString()).toBe(`${endpoint}/telemetry`); |
| 119 | } finally { |
| 120 | NodeFS.rmSync(home, { recursive: true, force: true }); |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | it("excludes tasks that explicitly disable caching from the hit-rate denominator", () => { |
| 125 | expect( |
| 126 | summarize({ |
| 127 | tasks: [ |
| 128 | task({ Spawned: { cache_status: "Disabled" } }), |
| 129 | task({ Spawned: { cache_status: { Miss: "changed" } } }), |
| 130 | ], |
| 131 | }), |
| 132 | ).toEqual([{ package: "@team/server", task: "typecheck", source: "executed", savedMs: 0 }]); |
| 133 | }); |
| 134 | |
| 135 | it("drops revoked credentials and continues delivering other queued endpoints", async () => { |
| 136 | const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-revoked-")); |
| 137 | try { |
| 138 | const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry"); |
| 139 | const state = NodePath.join(home, ".config/t3-task-cache"); |
| 140 | NodeFS.mkdirSync(queue, { recursive: true }); |
| 141 | NodeFS.mkdirSync(state, { recursive: true }); |
| 142 | NodeFS.writeFileSync( |
| 143 | NodePath.join(state, "credentials.json"), |
| 144 | JSON.stringify({ |
| 145 | "https://revoked.example": { name: "a", token: "old" }, |
| 146 | "https://current.example": { name: "a", token: "new" }, |
| 147 | }), |
| 148 | ); |
| 149 | for (const [file, endpoint] of [ |
| 150 | ["a.json", "https://revoked.example"], |
| 151 | ["b.json", "https://current.example"], |
| 152 | ]) |
| 153 | NodeFS.writeFileSync( |
| 154 | NodePath.join(queue, file!), |
| 155 | JSON.stringify({ endpoint, event: { id: file } }), |
| 156 | ); |
| 157 | using request = vi |
| 158 | .spyOn(globalThis, "fetch") |
| 159 | .mockResolvedValueOnce(new Response(null, { status: 403 })) |
| 160 | .mockResolvedValueOnce(new Response(null, { status: 204 })); |
| 161 | await flushTelemetry(home, {}); |
| 162 | expect(request).toHaveBeenCalledTimes(2); |
| 163 | expect(NodeFS.readdirSync(queue)).toEqual([]); |
| 164 | } finally { |
| 165 | NodeFS.rmSync(home, { recursive: true, force: true }); |
| 166 | } |
| 167 | }); |
| 168 | |
| 169 | it("delivers an event queued while the previous flusher releases its lock", async () => { |
| 170 | const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-handoff-")); |
| 171 | const endpoint = "https://cache.example/projects/t3code"; |
| 172 | try { |
| 173 | const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry"); |
| 174 | const state = NodePath.join(home, ".config/t3-task-cache"); |
| 175 | NodeFS.mkdirSync(queue, { recursive: true }); |
| 176 | NodeFS.mkdirSync(state, { recursive: true }); |
| 177 | NodeFS.writeFileSync( |
| 178 | NodePath.join(state, "credentials.json"), |
| 179 | JSON.stringify({ [endpoint]: { name: "alice", token: "secret-token" } }), |
| 180 | ); |
| 181 | using request = vi |
| 182 | .spyOn(globalThis, "fetch") |
| 183 | .mockImplementation(async () => new Response(null, { status: 204 })); |
| 184 | const rmdirSync = vi.mocked(NodeFS.rmdirSync); |
| 185 | const release = rmdirSync.getMockImplementation()!; |
| 186 | let competing: Promise<void> | undefined; |
| 187 | // The next command queues its event after this flusher's last scan, and its own flusher |
| 188 | // gives up because the lock is still held. |
| 189 | rmdirSync.mockImplementationOnce((path, options) => { |
| 190 | NodeFS.writeFileSync( |
| 191 | NodePath.join(queue, "late.json"), |
| 192 | JSON.stringify({ endpoint, event: { id: "late" } }), |
| 193 | ); |
| 194 | competing = flushTelemetry(home, {}); |
| 195 | release(path, options); |
| 196 | }); |
| 197 | await flushTelemetry(home, {}); |
| 198 | await competing; |
| 199 | expect(request).toHaveBeenCalledOnce(); |
| 200 | expect(NodeFS.readdirSync(queue)).toEqual([]); |
| 201 | } finally { |
| 202 | NodeFS.rmSync(home, { recursive: true, force: true }); |
| 203 | } |
| 204 | }); |