|
| 1 | +import { describe, expect, test, beforeEach, afterEach } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import os from "os" |
| 4 | +import fsp from "fs/promises" |
| 5 | + |
| 6 | +import { AltimateApi } from "../../src/altimate/api/client" |
| 7 | + |
| 8 | +const tmpRoot = path.join(os.tmpdir(), "sign-out-test-" + process.pid + "-" + Math.random().toString(36).slice(2)) |
| 9 | +const originalTestHome = process.env.OPENCODE_TEST_HOME |
| 10 | + |
| 11 | +const datamateEntry = (key: string) => ({ |
| 12 | + type: "remote", |
| 13 | + url: "https://mcp.test", |
| 14 | + oauth: false, |
| 15 | + headers: { Authorization: `Bearer ${key}`, "x-datamate-id": "1", "x-tenant": "tenant" }, |
| 16 | +}) |
| 17 | + |
| 18 | +async function writeJson(file: string, value: unknown) { |
| 19 | + await fsp.mkdir(path.dirname(file), { recursive: true }) |
| 20 | + await fsp.writeFile(file, JSON.stringify(value)) |
| 21 | +} |
| 22 | + |
| 23 | +const readJson = async (file: string) => JSON.parse(await fsp.readFile(file, "utf8")) |
| 24 | + |
| 25 | +describe("AltimateApi.signOut", () => { |
| 26 | + const home = path.join(tmpRoot, "home") |
| 27 | + const outside = path.join(tmpRoot, "outside") |
| 28 | + const repo = path.join(outside, "repo") |
| 29 | + const subdir = path.join(repo, "models", "staging") |
| 30 | + const rootConfig = path.join(repo, ".altimate-code", "altimate-code.json") |
| 31 | + const outsideConfig = path.join(outside, "altimate-code.json") |
| 32 | + |
| 33 | + beforeEach(async () => { |
| 34 | + process.env.OPENCODE_TEST_HOME = home |
| 35 | + await fsp.mkdir(path.join(repo, ".git"), { recursive: true }) |
| 36 | + await fsp.mkdir(subdir, { recursive: true }) |
| 37 | + }) |
| 38 | + |
| 39 | + afterEach(async () => { |
| 40 | + if (originalTestHome === undefined) delete process.env.OPENCODE_TEST_HOME |
| 41 | + else process.env.OPENCODE_TEST_HOME = originalTestHome |
| 42 | + await fsp.rm(tmpRoot, { recursive: true, force: true }).catch(() => {}) |
| 43 | + }) |
| 44 | + |
| 45 | + test("from a subdirectory, removes entries in configs up to the git root and no further", async () => { |
| 46 | + await writeJson(AltimateApi.credentialsPath(), { |
| 47 | + altimateUrl: "https://api.test", |
| 48 | + altimateInstanceName: "tenant", |
| 49 | + altimateApiKey: "gateway-test-key", |
| 50 | + }) |
| 51 | + await writeJson(rootConfig, { mcp: { "datamate-sales": datamateEntry("gateway-test-key") } }) |
| 52 | + await writeJson(outsideConfig, { mcp: { "datamate-other": datamateEntry("gateway-test-key") } }) |
| 53 | + |
| 54 | + const removed = await AltimateApi.signOut(subdir) |
| 55 | + |
| 56 | + expect(removed).toEqual([{ name: "datamate-sales", path: rootConfig }]) |
| 57 | + expect(await readJson(rootConfig)).toEqual({ mcp: {} }) |
| 58 | + // Above the git root, outside what a session in the repo loads. |
| 59 | + expect(await readJson(outsideConfig)).toEqual({ mcp: { "datamate-other": datamateEntry("gateway-test-key") } }) |
| 60 | + expect(await AltimateApi.isConfigured()).toBe(false) |
| 61 | + }) |
| 62 | + |
| 63 | + test("keeps the credential file and MCP entries when the saved key can't be read", async () => { |
| 64 | + delete process.env.SIGN_OUT_TEST_MISSING_KEY |
| 65 | + await writeJson(AltimateApi.credentialsPath(), { |
| 66 | + altimateUrl: "https://api.test", |
| 67 | + altimateInstanceName: "tenant", |
| 68 | + altimateApiKey: "${env:SIGN_OUT_TEST_MISSING_KEY}", |
| 69 | + }) |
| 70 | + await writeJson(rootConfig, { mcp: { "datamate-sales": datamateEntry("gateway-test-key") } }) |
| 71 | + |
| 72 | + await expect(AltimateApi.signOut(subdir)).rejects.toThrow("could not read the saved key") |
| 73 | + |
| 74 | + expect(await AltimateApi.isConfigured()).toBe(true) |
| 75 | + expect(await readJson(rootConfig)).toEqual({ mcp: { "datamate-sales": datamateEntry("gateway-test-key") } }) |
| 76 | + }) |
| 77 | + |
| 78 | + test("is a no-op when there is no credential file", async () => { |
| 79 | + await writeJson(rootConfig, { mcp: { "datamate-sales": datamateEntry("gateway-test-key") } }) |
| 80 | + |
| 81 | + expect(await AltimateApi.signOut(subdir)).toEqual([]) |
| 82 | + expect(await readJson(rootConfig)).toEqual({ mcp: { "datamate-sales": datamateEntry("gateway-test-key") } }) |
| 83 | + }) |
| 84 | +}) |
0 commit comments