Spaces:
Running
Running
File size: 5,047 Bytes
98c9143 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | import { afterEach, describe, expect, test } from "bun:test"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { fileURLToPath } from "node:url"
interface ConfigFileShape {
auth?: {
apiKeys?: Array<string>
adminApiKey?: string
}
modelMappings?: Record<string, string>
}
const cwd = fileURLToPath(new URL("../", import.meta.url))
const decoder = new TextDecoder()
const tempDirs: Array<string> = []
function createTempConfigDir(): string {
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), "copilot-api-admin-key-"),
)
tempDirs.push(tempDir)
return tempDir
}
function writeConfigFile(tempDir: string, config: ConfigFileShape): string {
const configPath = path.join(tempDir, "config.json")
fs.writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`, "utf8")
return configPath
}
function readConfigFile(configPath: string): ConfigFileShape {
return JSON.parse(fs.readFileSync(configPath, "utf8")) as ConfigFileShape
}
function runConfigScript(tempDir: string, script: string): void {
const result = Bun.spawnSync({
cmd: [process.execPath, "--eval", script],
cwd,
env: {
...process.env,
COPILOT_API_HOME: tempDir,
COPILOT_API_OAUTH_APP: "",
COPILOT_API_ENTERPRISE_URL: "",
},
})
if (result.exitCode !== 0) {
const stdout = decoder.decode(result.stdout)
const stderr = decoder.decode(result.stderr)
throw new Error(
`Config script failed with exit code ${result.exitCode}\nstdout:\n${stdout}\nstderr:\n${stderr}`,
)
}
}
afterEach(() => {
while (tempDirs.length > 0) {
fs.rmSync(tempDirs.pop()!, { recursive: true, force: true })
}
})
describe("config admin api key", () => {
test("generates and persists an admin api key when missing", () => {
const tempDir = createTempConfigDir()
const configPath = writeConfigFile(tempDir, {
auth: {
apiKeys: ["regular-key"],
},
modelMappings: {
"claude-opus-4-7": "gpt-5-mini",
},
})
runConfigScript(
tempDir,
'const { mergeConfigWithDefaults } = await import("./src/lib/config"); mergeConfigWithDefaults();',
)
const config = readConfigFile(configPath)
expect(config.auth?.apiKeys).toEqual(["regular-key"])
expect(typeof config.auth?.adminApiKey).toBe("string")
expect(config.auth?.adminApiKey?.length).toBeGreaterThan(0)
expect(config.modelMappings).toEqual({
"claude-opus-4-7": "gpt-5-mini",
})
})
test("keeps an existing admin api key stable across startup merges", () => {
const tempDir = createTempConfigDir()
const configPath = writeConfigFile(tempDir, {
auth: {
apiKeys: ["regular-key"],
adminApiKey: "existing-admin-key",
},
})
runConfigScript(
tempDir,
'const { mergeConfigWithDefaults } = await import("./src/lib/config"); mergeConfigWithDefaults();',
)
const config = readConfigFile(configPath)
expect(config.auth?.adminApiKey).toBe("existing-admin-key")
})
test("preserves the generated admin api key when model mappings are updated", () => {
const tempDir = createTempConfigDir()
const configPath = writeConfigFile(tempDir, {
auth: {
apiKeys: ["regular-key"],
},
})
runConfigScript(
tempDir,
'const { mergeConfigWithDefaults } = await import("./src/lib/config"); mergeConfigWithDefaults();',
)
const generatedAdminApiKey = readConfigFile(configPath).auth?.adminApiKey
runConfigScript(
tempDir,
'const { mergeConfigWithDefaults, setModelMappings } = await import("./src/lib/config"); mergeConfigWithDefaults(); setModelMappings({ "claude-opus-4-7": "dash/qwen-plus" });',
)
const config = readConfigFile(configPath)
expect(config.auth?.adminApiKey).toBe(generatedAdminApiKey)
expect(config.modelMappings).toEqual({
"claude-opus-4-7": "dash/qwen-plus",
})
})
test("regenerates an admin api key if model mappings are saved after the key is removed", () => {
const tempDir = createTempConfigDir()
const configPath = writeConfigFile(tempDir, {
auth: {
apiKeys: ["regular-key"],
},
})
runConfigScript(
tempDir,
'const { mergeConfigWithDefaults } = await import("./src/lib/config"); mergeConfigWithDefaults();',
)
const generatedAdminApiKey = readConfigFile(configPath).auth?.adminApiKey
writeConfigFile(tempDir, {
auth: {
apiKeys: ["regular-key"],
},
})
runConfigScript(
tempDir,
'const { setModelMappings } = await import("./src/lib/config"); setModelMappings({ "claude-opus-4-7": "gpt-5-mini" });',
)
const config = readConfigFile(configPath)
expect(config.auth?.adminApiKey).not.toBeUndefined()
expect(config.auth?.adminApiKey?.length).toBeGreaterThan(0)
expect(config.auth?.adminApiKey).not.toBe(generatedAdminApiKey)
expect(config.modelMappings).toEqual({
"claude-opus-4-7": "gpt-5-mini",
})
})
})
|