Spaces:
Sleeping
Sleeping
File size: 5,239 Bytes
f395b70 | 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { Readable } from "node:stream";
import { ConfigSwitchStore } from "./config-switch-store.mjs";
import { createRequestListener } from "./server.mjs";
async function makeTempRoot() {
return fs.mkdtemp(path.join(os.tmpdir(), "local-ui-server-test-"));
}
function makeProviderDefinitions(root) {
return {
codex: {
provider: "codex",
label: "Codex 配置",
targetPaths: [
{
key: "authJsonText",
label: "auth.json",
path: path.join(root, "home", ".codex", "auth.json"),
kind: "json",
},
{
key: "configTomlText",
label: "config.toml",
path: path.join(root, "home", ".codex", "config.toml"),
kind: "toml",
},
],
},
"claude-code": {
provider: "claude-code",
label: "Claude Code 配置",
targetPaths: [
{
key: "settingsJsonText",
label: "settings.json",
path: path.join(root, "home", ".claude", "settings.json"),
kind: "json",
},
],
},
};
}
async function makeConfigSwitchListener() {
const root = await makeTempRoot();
const configSwitchStore = new ConfigSwitchStore(path.join(root, "data"), {
providerDefinitions: makeProviderDefinitions(root),
});
await configSwitchStore.load();
return {
root,
configSwitchStore,
listener: createRequestListener({
staticDir: path.join(root, "static"),
historyStore: { list: async () => [] },
poolStore: {
listPools: () => [],
loadPool: async () => {
throw new Error("not implemented");
},
savePool: async () => {
throw new Error("not implemented");
},
validatePoolItems: () => ({ ok: true, errors: [], normalizedItems: [] }),
},
runManager: {
execute: async () => {
throw new Error("not implemented");
},
getRun: () => null,
},
proxyManager: {
start: async () => ({ reused: false, status: {} }),
stop: async () => ({}),
getStatus: async () => ({}),
},
apiPoolProxyManagerCodex: {
start: async () => ({ reused: false, status: {} }),
stop: async () => ({}),
getStatus: async () => ({}),
},
apiPoolProxyManagerClaude: {
start: async () => ({ reused: false, status: {} }),
stop: async () => ({}),
getStatus: async () => ({}),
},
configSwitchStore,
}),
};
}
async function invoke(listener, { method, url, body }) {
const req = Readable.from(
body == null ? [] : [Buffer.from(JSON.stringify(body), "utf8")],
);
req.method = method;
req.url = url;
return new Promise((resolve, reject) => {
const res = {
statusCode: 200,
headers: {},
setHeader(name, value) {
this.headers[name.toLowerCase()] = value;
},
end(chunk) {
const text = Buffer.isBuffer(chunk)
? chunk.toString("utf8")
: String(chunk || "");
resolve({
statusCode: this.statusCode,
headers: this.headers,
body: text ? JSON.parse(text) : null,
});
},
};
listener(req, res).catch(reject);
});
}
test("local ui server exposes config-switch GET and POST routes", async () => {
const { listener } = await makeConfigSwitchListener();
const created = await invoke(listener, {
method: "POST",
url: "/api/config-switch/codex",
body: {
name: "主配置",
payload: {
authJsonText: '{"OPENAI_API_KEY":"sk-main"}',
configTomlText: 'model_provider = "OpenAI"\nmodel = "gpt-5.4"',
},
},
});
assert.equal(created.statusCode, 201);
assert.equal(created.body.providers.codex.presets.length, 1);
const fetched = await invoke(listener, {
method: "GET",
url: "/api/config-switch",
});
assert.equal(fetched.statusCode, 200);
assert.equal(fetched.body.providers.codex.presets[0].name, "主配置");
});
test("local ui server returns 4xx for invalid config-switch provider and missing confirmation", async () => {
const { listener } = await makeConfigSwitchListener();
const invalidProvider = await invoke(listener, {
method: "POST",
url: "/api/config-switch/missing",
body: {
name: "bad",
payload: {
authJsonText: '{"OPENAI_API_KEY":"sk"}',
configTomlText: 'model = "gpt-5.4"',
},
},
});
assert.equal(invalidProvider.statusCode, 404);
const created = await invoke(listener, {
method: "POST",
url: "/api/config-switch/codex",
body: {
name: "主配置",
payload: {
authJsonText: '{"OPENAI_API_KEY":"sk-main"}',
configTomlText: 'model_provider = "OpenAI"',
},
},
});
const presetId = created.body.providers.codex.presets[0].id;
const activateWithoutConfirm = await invoke(listener, {
method: "POST",
url: `/api/config-switch/codex/${presetId}/activate`,
body: {},
});
assert.equal(activateWithoutConfirm.statusCode, 409);
});
|