|
| 1 | +// altimate_change - new file |
| 2 | +// |
| 3 | +// Unit coverage for the --workspace launch resolver. Exercises the pure |
| 4 | +// name-match helper directly, and the wired-up resolveWorkspaceForLaunch |
| 5 | +// with the local binding cache + ALTIMATE_WORKSPACE flag stubbed. Does |
| 6 | +// NOT exercise the tui.ts wiring or the worker subprocess env-var pickup |
| 7 | +// — those are integration territory. |
| 8 | +import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 9 | +import { mkdirSync, rmSync } from "node:fs" |
| 10 | +import path from "node:path" |
| 11 | +import os from "node:os" |
| 12 | + |
| 13 | +// Redirect Global.Path.state BEFORE importing state.ts so cachePath() |
| 14 | +// resolves inside the sandbox. Same isolation pattern as workspace.test.ts. |
| 15 | +const ORIGINAL_XDG_STATE_HOME = process.env.XDG_STATE_HOME |
| 16 | +const SANDBOX = path.join( |
| 17 | + os.tmpdir(), |
| 18 | + `altimate-launch-resolve-test-${process.pid}-${Date.now()}`, |
| 19 | +) |
| 20 | +mkdirSync(path.join(SANDBOX, "state"), { recursive: true }) |
| 21 | +process.env.XDG_STATE_HOME = path.join(SANDBOX, "state") |
| 22 | +afterAll(() => { |
| 23 | + if (ORIGINAL_XDG_STATE_HOME === undefined) delete process.env.XDG_STATE_HOME |
| 24 | + else process.env.XDG_STATE_HOME = ORIGINAL_XDG_STATE_HOME |
| 25 | + try { |
| 26 | + rmSync(SANDBOX, { recursive: true, force: true }) |
| 27 | + } catch { |
| 28 | + /* best effort */ |
| 29 | + } |
| 30 | +}) |
| 31 | + |
| 32 | +const { nameMatches, resolveWorkspaceForLaunch } = await import( |
| 33 | + "../../../src/altimate/workspace/launch-resolve" |
| 34 | +) |
| 35 | +const { getResolvedWorkspaceId, setResolvedWorkspaceId } = await import( |
| 36 | + "../../../src/altimate/workspace/session-context" |
| 37 | +) |
| 38 | +const { recordApprovedBinding, cachePath } = await import( |
| 39 | + "../../../src/altimate/workspace/state" |
| 40 | +) |
| 41 | + |
| 42 | +// Stub credentials so state.ts's tenant/apiUrl scoping is deterministic. |
| 43 | +import { AltimateApi } from "../../../src/altimate/api/client" |
| 44 | +type Creds = Awaited<ReturnType<typeof AltimateApi.getCredentials>> |
| 45 | +const originalIsConfigured = AltimateApi.isConfigured |
| 46 | +const originalGetCreds = AltimateApi.getCredentials |
| 47 | +function stubCreds() { |
| 48 | + ;(AltimateApi as unknown as { isConfigured: () => Promise<boolean> }).isConfigured = async () => |
| 49 | + true |
| 50 | + ;(AltimateApi as unknown as { getCredentials: () => Promise<Creds> }).getCredentials = |
| 51 | + async () => |
| 52 | + ({ |
| 53 | + altimateInstanceName: "altimate2", |
| 54 | + altimateUrl: "http://localhost:5001", |
| 55 | + altimateApiKey: "dummy", |
| 56 | + }) as Creds |
| 57 | +} |
| 58 | +function unstubCreds() { |
| 59 | + ;(AltimateApi as unknown as { isConfigured: typeof originalIsConfigured }).isConfigured = |
| 60 | + originalIsConfigured |
| 61 | + ;(AltimateApi as unknown as { getCredentials: typeof originalGetCreds }).getCredentials = |
| 62 | + originalGetCreds |
| 63 | +} |
| 64 | + |
| 65 | +beforeEach(() => { |
| 66 | + stubCreds() |
| 67 | + setResolvedWorkspaceId(null) |
| 68 | + // Clean cache file between tests so state doesn't leak across cases. |
| 69 | + try { |
| 70 | + rmSync(cachePath(), { force: true }) |
| 71 | + } catch { |
| 72 | + /* best effort */ |
| 73 | + } |
| 74 | + // Explicitly enable the pilot flag for every test. Restored per-test in |
| 75 | + // afterEach so the "flag off" test can override. |
| 76 | + process.env.ALTIMATE_WORKSPACE = "1" |
| 77 | +}) |
| 78 | +afterEach(() => { |
| 79 | + unstubCreds() |
| 80 | + setResolvedWorkspaceId(null) |
| 81 | + delete process.env.ALTIMATE_WORKSPACE |
| 82 | +}) |
| 83 | + |
| 84 | +describe("nameMatches", () => { |
| 85 | + const binding = { |
| 86 | + datamateId: 42, |
| 87 | + datamateName: "Growth", |
| 88 | + repoRemote: "ssh://[email protected]/foo/bar", |
| 89 | + projectPath: "/tmp/foo", |
| 90 | + linkedAt: 0, |
| 91 | + } |
| 92 | + |
| 93 | + test("exact match", () => { |
| 94 | + expect(nameMatches("Growth", binding)).toBe(true) |
| 95 | + }) |
| 96 | + test("case-insensitive match", () => { |
| 97 | + expect(nameMatches("GROWTH", binding)).toBe(true) |
| 98 | + expect(nameMatches("growth", binding)).toBe(true) |
| 99 | + }) |
| 100 | + test("whitespace-trimmed match", () => { |
| 101 | + expect(nameMatches(" Growth ", binding)).toBe(true) |
| 102 | + }) |
| 103 | + test("mismatch", () => { |
| 104 | + expect(nameMatches("Other", binding)).toBe(false) |
| 105 | + }) |
| 106 | + test("substring is not a hit (no fuzzy match)", () => { |
| 107 | + expect(nameMatches("Grow", binding)).toBe(false) |
| 108 | + expect(nameMatches("Growthy", binding)).toBe(false) |
| 109 | + }) |
| 110 | +}) |
| 111 | + |
| 112 | +describe("resolveWorkspaceForLaunch", () => { |
| 113 | + const DIRECTORY = path.join(SANDBOX, "project") |
| 114 | + |
| 115 | + beforeEach(async () => { |
| 116 | + mkdirSync(DIRECTORY, { recursive: true }) |
| 117 | + await recordApprovedBinding(DIRECTORY, { |
| 118 | + datamateId: 42, |
| 119 | + datamateName: "Growth", |
| 120 | + repoRemote: null, |
| 121 | + projectPath: DIRECTORY, |
| 122 | + linkedAt: 0, |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + test("no --workspace arg → no env var set, no-op", async () => { |
| 127 | + await resolveWorkspaceForLaunch(DIRECTORY, undefined) |
| 128 | + expect(getResolvedWorkspaceId()).toBeNull() |
| 129 | + }) |
| 130 | + |
| 131 | + test("ALTIMATE_WORKSPACE flag off → no env var set even when --workspace given", async () => { |
| 132 | + delete process.env.ALTIMATE_WORKSPACE |
| 133 | + await resolveWorkspaceForLaunch(DIRECTORY, "Growth") |
| 134 | + expect(getResolvedWorkspaceId()).toBeNull() |
| 135 | + }) |
| 136 | + |
| 137 | + test("matching name → env var set to the binding's datamateId", async () => { |
| 138 | + await resolveWorkspaceForLaunch(DIRECTORY, "Growth") |
| 139 | + expect(getResolvedWorkspaceId()).toBe(42) |
| 140 | + }) |
| 141 | + |
| 142 | + test("case-insensitive matching name → env var set", async () => { |
| 143 | + await resolveWorkspaceForLaunch(DIRECTORY, "growth") |
| 144 | + expect(getResolvedWorkspaceId()).toBe(42) |
| 145 | + }) |
| 146 | + |
| 147 | + test("mismatched name → env var STILL set (attaches to linked workspace with a note per AI-8504 spec)", async () => { |
| 148 | + await resolveWorkspaceForLaunch(DIRECTORY, "Other") |
| 149 | + expect(getResolvedWorkspaceId()).toBe(42) |
| 150 | + }) |
| 151 | + |
| 152 | + test("no binding for this directory → env var NOT set (fail-fast)", async () => { |
| 153 | + const unlinkedDir = path.join(SANDBOX, "unlinked") |
| 154 | + mkdirSync(unlinkedDir, { recursive: true }) |
| 155 | + await resolveWorkspaceForLaunch(unlinkedDir, "Growth") |
| 156 | + expect(getResolvedWorkspaceId()).toBeNull() |
| 157 | + }) |
| 158 | +}) |
| 159 | + |
| 160 | +describe("session-context env-var round trip", () => { |
| 161 | + test("set/get/clear cycle", () => { |
| 162 | + setResolvedWorkspaceId(123) |
| 163 | + expect(getResolvedWorkspaceId()).toBe(123) |
| 164 | + setResolvedWorkspaceId(null) |
| 165 | + expect(getResolvedWorkspaceId()).toBeNull() |
| 166 | + }) |
| 167 | + |
| 168 | + test("malformed env value returns null (defense against poisoned env)", () => { |
| 169 | + process.env.ALTIMATE_RESOLVED_WORKSPACE_ID = "not-a-number" |
| 170 | + expect(getResolvedWorkspaceId()).toBeNull() |
| 171 | + process.env.ALTIMATE_RESOLVED_WORKSPACE_ID = "0" |
| 172 | + expect(getResolvedWorkspaceId()).toBeNull() |
| 173 | + process.env.ALTIMATE_RESOLVED_WORKSPACE_ID = "-5" |
| 174 | + expect(getResolvedWorkspaceId()).toBeNull() |
| 175 | + delete process.env.ALTIMATE_RESOLVED_WORKSPACE_ID |
| 176 | + }) |
| 177 | +}) |
0 commit comments