|
| 1 | +// altimate_change - new file |
| 2 | +// |
| 3 | +// `/workspace` Sync follows the IDE extension's pin. `Manage.sync` read only the on-disk link, so a |
| 4 | +// `serve` pinned by the extension answered "not linked" for the workspace it was pinned to, while |
| 5 | +// the per-write mirror (which resolves through the pin) sent to it. These cover which binding the |
| 6 | +// sweep runs against; the sweep itself is covered by manage.test.ts and memory-sync.test.ts. |
| 7 | +import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 8 | +import { mkdirSync, rmSync } from "node:fs" |
| 9 | +import path from "node:path" |
| 10 | +import os from "node:os" |
| 11 | + |
| 12 | +const ORIGINAL_XDG_STATE_HOME = process.env.XDG_STATE_HOME |
| 13 | +const ORIGINAL_PILOT = process.env.ALTIMATE_WORKSPACE |
| 14 | +const SANDBOX = path.join(os.tmpdir(), `altimate-manage-pin-test-${process.pid}-${Date.now()}`) |
| 15 | +mkdirSync(path.join(SANDBOX, "state"), { recursive: true }) |
| 16 | +process.env.XDG_STATE_HOME = path.join(SANDBOX, "state") |
| 17 | + |
| 18 | +const { recordApprovedBinding, __resetPinValidation } = await import("../../../src/altimate/workspace/state") |
| 19 | +const { sync } = await import("../../../src/altimate/workspace/manage") |
| 20 | +const { resetEnablementMemoForTests } = await import("../../../src/altimate/workspace/memory-sync") |
| 21 | +const { AltimateApi } = await import("../../../src/altimate/api/client") |
| 22 | +const { WorkspaceApi } = await import("../../../src/altimate/workspace/api-client") |
| 23 | + |
| 24 | +const ROOT = path.join(SANDBOX, "project") |
| 25 | +const OUTSIDE = path.join(SANDBOX, "elsewhere") |
| 26 | +mkdirSync(ROOT, { recursive: true }) |
| 27 | +mkdirSync(OUTSIDE, { recursive: true }) |
| 28 | + |
| 29 | +const originalIsConfigured = AltimateApi.isConfigured |
| 30 | +const originalGetCreds = AltimateApi.getCredentials |
| 31 | +const originalList = WorkspaceApi.listDatamates |
| 32 | +type Creds = Awaited<ReturnType<typeof AltimateApi.getCredentials>> |
| 33 | + |
| 34 | +const PIN_VARS = [ |
| 35 | + "ALTIMATE_CODE_SERVE", |
| 36 | + "ALTIMATE_PINNED_WORKSPACE_ID", |
| 37 | + "ALTIMATE_PINNED_WORKSPACE_NAME", |
| 38 | + "ALTIMATE_PINNED_WORKSPACE_ROOT", |
| 39 | +] |
| 40 | + |
| 41 | +/** The pinned workspace has memory ON and the project's own link has it OFF, so the gate reason |
| 42 | + * says which of the two the sweep ran against. */ |
| 43 | +const WORKSPACES = [ |
| 44 | + { id: 42, name: "pinned-workspace", memoryEnabled: true }, |
| 45 | + { id: 7, name: "project-link", memoryEnabled: false }, |
| 46 | +] |
| 47 | + |
| 48 | +function setPin(id = "42") { |
| 49 | + process.env.ALTIMATE_CODE_SERVE = "1" |
| 50 | + process.env.ALTIMATE_PINNED_WORKSPACE_ID = id |
| 51 | + process.env.ALTIMATE_PINNED_WORKSPACE_NAME = "pinned-workspace" |
| 52 | + process.env.ALTIMATE_PINNED_WORKSPACE_ROOT = ROOT |
| 53 | +} |
| 54 | + |
| 55 | +function clearPin() { |
| 56 | + for (const k of PIN_VARS) delete process.env[k] |
| 57 | +} |
| 58 | + |
| 59 | +async function seedLocalLink(directory = ROOT) { |
| 60 | + await recordApprovedBinding(directory, { |
| 61 | + datamateId: 7, |
| 62 | + datamateName: "project-link", |
| 63 | + linkedAt: Date.now(), |
| 64 | + repoRemote: "[email protected]:acme/project.git", |
| 65 | + projectPath: null, |
| 66 | + } as never) |
| 67 | +} |
| 68 | + |
| 69 | +beforeEach(() => { |
| 70 | + process.env.ALTIMATE_WORKSPACE = "1" |
| 71 | + __resetPinValidation() |
| 72 | + resetEnablementMemoForTests() |
| 73 | + ;(AltimateApi as unknown as { isConfigured: () => Promise<boolean> }).isConfigured = async () => true |
| 74 | + ;(AltimateApi as unknown as { getCredentials: () => Promise<Creds> }).getCredentials = async () => |
| 75 | + ({ altimateInstanceName: "acme", altimateUrl: "https://api.test", altimateApiKey: "k" }) as Creds |
| 76 | + ;(WorkspaceApi as unknown as { listDatamates: () => Promise<unknown> }).listDatamates = async () => WORKSPACES |
| 77 | + clearPin() |
| 78 | +}) |
| 79 | + |
| 80 | +afterEach(() => { |
| 81 | + clearPin() |
| 82 | +}) |
| 83 | + |
| 84 | +afterAll(() => { |
| 85 | + ;(AltimateApi as unknown as { isConfigured: typeof originalIsConfigured }).isConfigured = originalIsConfigured |
| 86 | + ;(AltimateApi as unknown as { getCredentials: typeof originalGetCreds }).getCredentials = originalGetCreds |
| 87 | + ;(WorkspaceApi as unknown as { listDatamates: typeof originalList }).listDatamates = originalList |
| 88 | + if (ORIGINAL_XDG_STATE_HOME === undefined) delete process.env.XDG_STATE_HOME |
| 89 | + else process.env.XDG_STATE_HOME = ORIGINAL_XDG_STATE_HOME |
| 90 | + if (ORIGINAL_PILOT === undefined) delete process.env.ALTIMATE_WORKSPACE |
| 91 | + else process.env.ALTIMATE_WORKSPACE = ORIGINAL_PILOT |
| 92 | + rmSync(SANDBOX, { recursive: true, force: true }) |
| 93 | +}) |
| 94 | + |
| 95 | +describe("sync under an IDE pin", () => { |
| 96 | + test("runs against the pinned workspace in a project that was never linked", async () => { |
| 97 | + setPin() |
| 98 | + const report = await sync(ROOT) |
| 99 | + expect(report.gated).toBe(false) |
| 100 | + expect(report.gatedBecause).toBeUndefined() |
| 101 | + }) |
| 102 | + |
| 103 | + test("the pin outranks the project's own link", async () => { |
| 104 | + await seedLocalLink() |
| 105 | + setPin() |
| 106 | + // The local link (7) has memory off and would gate; the pin (42) has it on. |
| 107 | + expect((await sync(ROOT)).gated).toBe(false) |
| 108 | + }) |
| 109 | + |
| 110 | + test("a pin naming a workspace this account cannot see fails closed, not onto the local link", async () => { |
| 111 | + await seedLocalLink() |
| 112 | + setPin("99") |
| 113 | + const report = await sync(ROOT) |
| 114 | + expect(report.gated).toBe(true) |
| 115 | + expect(report.gatedBecause).toBe("no-binding") |
| 116 | + }) |
| 117 | + |
| 118 | + test("a directory outside the pinned root is not treated as pinned", async () => { |
| 119 | + setPin() |
| 120 | + const report = await sync(OUTSIDE) |
| 121 | + expect(report.gated).toBe(true) |
| 122 | + expect(report.gatedBecause).toBe("no-binding") |
| 123 | + }) |
| 124 | +}) |
| 125 | + |
| 126 | +describe("sync without a pin", () => { |
| 127 | + test("still reads the project's own link", async () => { |
| 128 | + await seedLocalLink() |
| 129 | + const report = await sync(ROOT) |
| 130 | + // Reached the sweep with link 7, whose memory is off. |
| 131 | + expect(report.gated).toBe(true) |
| 132 | + expect(report.gatedBecause).toBe("memory-off") |
| 133 | + }) |
| 134 | + |
| 135 | + test("an unlinked project is still gated on the missing binding", async () => { |
| 136 | + // A directory no test links: the binding cache is process-memoized, so `ROOT` may still hold one. |
| 137 | + const report = await sync(OUTSIDE) |
| 138 | + expect(report.gatedBecause).toBe("no-binding") |
| 139 | + }) |
| 140 | +}) |
0 commit comments