|
| 1 | +// altimate_change - new file |
| 2 | +// |
| 3 | +// The IDE extension's pin governs warehouse tool ROUTING, not just identity, skills and memory. |
| 4 | +// |
| 5 | +// `state-pin.test.ts` covers the pin inside `resolveBindingOutcome`, which is what skills, memory |
| 6 | +// and the identity section read. Routing reads elsewhere — `engine-probes.resolveBinding` and |
| 7 | +// `precedence.currentBinding` went straight to the on-disk cache — so a pinned session could name |
| 8 | +// one workspace in the identity section and route warehouse calls at another (#1337). These cover |
| 9 | +// the routing side of that precedence, and the refusal, which is where the damage would be. |
| 10 | +import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 11 | +import { mkdirSync, rmSync } from "node:fs" |
| 12 | +import path from "node:path" |
| 13 | +import os from "node:os" |
| 14 | + |
| 15 | +const ORIGINAL_XDG_STATE_HOME = process.env.XDG_STATE_HOME |
| 16 | +const SANDBOX = path.join(os.tmpdir(), `altimate-routing-pin-test-${process.pid}-${Date.now()}`) |
| 17 | +mkdirSync(path.join(SANDBOX, "state"), { recursive: true }) |
| 18 | +process.env.XDG_STATE_HOME = path.join(SANDBOX, "state") |
| 19 | + |
| 20 | +const { resolvePinnedBindingForRouting, recordApprovedBinding, __resetPinValidation } = await import( |
| 21 | + "../../../src/altimate/workspace/state" |
| 22 | +) |
| 23 | +const { resolveBinding } = await import("../../../src/altimate/workspace/engine-probes") |
| 24 | +const { AltimateApi } = await import("../../../src/altimate/api/client") |
| 25 | +const { WorkspaceApi } = await import("../../../src/altimate/workspace/api-client") |
| 26 | + |
| 27 | +const ROOT = path.join(SANDBOX, "project") |
| 28 | +mkdirSync(ROOT, { recursive: true }) |
| 29 | + |
| 30 | +const originalIsConfigured = AltimateApi.isConfigured |
| 31 | +const originalGetCreds = AltimateApi.getCredentials |
| 32 | +const originalList = WorkspaceApi.listDatamates |
| 33 | +type Creds = Awaited<ReturnType<typeof AltimateApi.getCredentials>> |
| 34 | + |
| 35 | +function stubCreds() { |
| 36 | + ;(AltimateApi as unknown as { isConfigured: () => Promise<boolean> }).isConfigured = async () => true |
| 37 | + ;(AltimateApi as unknown as { getCredentials: () => Promise<Creds> }).getCredentials = async () => |
| 38 | + ({ altimateInstanceName: "acme", altimateUrl: "https://api.test", altimateApiKey: "k" }) as Creds |
| 39 | +} |
| 40 | + |
| 41 | +function stubList(rows: { id: number; name: string }[]) { |
| 42 | + ;(WorkspaceApi as unknown as { listDatamates: () => Promise<unknown> }).listDatamates = async () => rows |
| 43 | +} |
| 44 | + |
| 45 | +const PIN_VARS = [ |
| 46 | + "ALTIMATE_CODE_SERVE", |
| 47 | + "ALTIMATE_PINNED_WORKSPACE_ID", |
| 48 | + "ALTIMATE_PINNED_WORKSPACE_NAME", |
| 49 | + "ALTIMATE_PINNED_WORKSPACE_ROOT", |
| 50 | +] |
| 51 | + |
| 52 | +function setPin(over: Record<string, string | undefined> = {}) { |
| 53 | + const base: Record<string, string | undefined> = { |
| 54 | + ALTIMATE_CODE_SERVE: "1", |
| 55 | + ALTIMATE_PINNED_WORKSPACE_ID: "42", |
| 56 | + ALTIMATE_PINNED_WORKSPACE_NAME: "pinned-workspace", |
| 57 | + ALTIMATE_PINNED_WORKSPACE_ROOT: ROOT, |
| 58 | + ...over, |
| 59 | + } |
| 60 | + for (const [k, v] of Object.entries(base)) { |
| 61 | + if (v === undefined) delete process.env[k] |
| 62 | + else process.env[k] = v |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function clearPin() { |
| 67 | + for (const k of PIN_VARS) delete process.env[k] |
| 68 | +} |
| 69 | + |
| 70 | +/** The project's own link, naming a DIFFERENT workspace than the pin — the returning-user case |
| 71 | + * from the report, where identity said one id and routing said another. */ |
| 72 | +async function seedLocalLink(datamateId = 7, datamateName = "project-link") { |
| 73 | + await recordApprovedBinding(ROOT, { |
| 74 | + datamateId, |
| 75 | + datamateName, |
| 76 | + linkedAt: Date.now(), |
| 77 | + repoRemote: "[email protected]:acme/project.git", |
| 78 | + // Both identity keys are written explicitly: the strict reader rejects a row where either is |
| 79 | + // `undefined` (it accepts `string | null`), so omitting one produces a cache the routing read |
| 80 | + // cannot parse — which looks like a product failure in a test that is only mis-seeded. |
| 81 | + projectPath: null, |
| 82 | + } as never) |
| 83 | +} |
| 84 | + |
| 85 | +beforeEach(() => { |
| 86 | + __resetPinValidation() |
| 87 | + stubCreds() |
| 88 | + stubList([ |
| 89 | + { id: 42, name: "pinned-workspace" }, |
| 90 | + { id: 7, name: "project-link" }, |
| 91 | + ]) |
| 92 | + clearPin() |
| 93 | +}) |
| 94 | + |
| 95 | +afterEach(() => { |
| 96 | + clearPin() |
| 97 | + __resetPinValidation() |
| 98 | +}) |
| 99 | + |
| 100 | +afterAll(() => { |
| 101 | + ;(AltimateApi as unknown as { isConfigured: unknown }).isConfigured = originalIsConfigured |
| 102 | + ;(AltimateApi as unknown as { getCredentials: unknown }).getCredentials = originalGetCreds |
| 103 | + ;(WorkspaceApi as unknown as { listDatamates: unknown }).listDatamates = originalList |
| 104 | + if (ORIGINAL_XDG_STATE_HOME === undefined) delete process.env.XDG_STATE_HOME |
| 105 | + else process.env.XDG_STATE_HOME = ORIGINAL_XDG_STATE_HOME |
| 106 | + rmSync(SANDBOX, { recursive: true, force: true }) |
| 107 | +}) |
| 108 | + |
| 109 | +describe("resolvePinnedBindingForRouting", () => { |
| 110 | + test("answers null with no pin, so unpinned sessions keep reading their own link", async () => { |
| 111 | + expect(await resolvePinnedBindingForRouting(ROOT)).toBeNull() |
| 112 | + }) |
| 113 | + |
| 114 | + test("returns the pinned workspace when the account can see it", async () => { |
| 115 | + setPin() |
| 116 | + const outcome = await resolvePinnedBindingForRouting(ROOT) |
| 117 | + expect(outcome?.status).toBe("bound") |
| 118 | + expect(outcome?.status === "bound" && outcome.binding.datamateId).toBe(42) |
| 119 | + }) |
| 120 | + |
| 121 | + test("refuses a malformed pin rather than falling through to the project's link", async () => { |
| 122 | + setPin({ ALTIMATE_PINNED_WORKSPACE_ID: "not-a-number" }) |
| 123 | + expect((await resolvePinnedBindingForRouting(ROOT))?.status).toBe("unknown") |
| 124 | + }) |
| 125 | + |
| 126 | + test("refuses a pin naming a workspace this account cannot see", async () => { |
| 127 | + stubList([{ id: 7, name: "project-link" }]) |
| 128 | + setPin() |
| 129 | + expect((await resolvePinnedBindingForRouting(ROOT))?.status).toBe("unknown") |
| 130 | + }) |
| 131 | + |
| 132 | + test("refuses a pin for a directory outside the pinned root", async () => { |
| 133 | + setPin() |
| 134 | + expect((await resolvePinnedBindingForRouting(path.join(SANDBOX, "elsewhere")))?.status).toBe("unknown") |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +describe("engine-probes.resolveBinding — the routing read", () => { |
| 139 | + test("routes at the pin, not the project's own link", async () => { |
| 140 | + await seedLocalLink(7) |
| 141 | + setPin() |
| 142 | + const read = await resolveBinding(ROOT) |
| 143 | + expect(read.kind).toBe("bound") |
| 144 | + // The regression: this returned 7 — the identity section said 42 in the same prompt. |
| 145 | + expect(read.kind === "bound" && read.binding.datamateId).toBe(42) |
| 146 | + }) |
| 147 | + |
| 148 | + test("still reads the project's own link when nothing is pinned", async () => { |
| 149 | + await seedLocalLink(7) |
| 150 | + const read = await resolveBinding(ROOT) |
| 151 | + expect(read.kind).toBe("bound") |
| 152 | + expect(read.kind === "bound" && read.binding.datamateId).toBe(7) |
| 153 | + }) |
| 154 | + |
| 155 | + test("fails closed when the pin cannot be honoured, rather than routing at the project's link", async () => { |
| 156 | + await seedLocalLink(7) |
| 157 | + stubList([{ id: 7, name: "project-link" }]) |
| 158 | + setPin() |
| 159 | + const read = await resolveBinding(ROOT) |
| 160 | + // Not `bound` at 7: falling back to the project's link is the confusion this prevents. |
| 161 | + expect(read.kind).toBe("failed") |
| 162 | + }) |
| 163 | + |
| 164 | + test("carries the credential scope so the engine key stays account-partitioned", async () => { |
| 165 | + setPin() |
| 166 | + const read = await resolveBinding(ROOT) |
| 167 | + expect(read.kind === "bound" && read.binding.scope).toBe("acme|https://api.test") |
| 168 | + }) |
| 169 | +}) |
0 commit comments