-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathwriteProxyConfig.test.mjs
More file actions
246 lines (208 loc) · 7 KB
/
Copy pathwriteProxyConfig.test.mjs
File metadata and controls
246 lines (208 loc) · 7 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
const mainPath = fileURLToPath(new URL("../dist/main.js", import.meta.url));
const MODEL_PROVIDER = "codex-action-responses-proxy";
const MANAGED_MODEL_PROVIDER_START =
"# BEGIN action-managed model provider config";
const MANAGED_MODEL_PROVIDER_END =
"# END action-managed model provider config";
const MANAGED_PROXY_PROVIDER_START =
"# BEGIN action-managed proxy provider config";
const MANAGED_PROXY_PROVIDER_END =
"# END action-managed proxy provider config";
function runWriteProxyConfig(home, port) {
const result = spawnSync(
process.execPath,
[
mainPath,
"write-proxy-config",
"--codex-home",
home,
"--port",
String(port),
"--safety-strategy",
"unsafe",
],
{ encoding: "utf8" }
);
assert.equal(result.status, 0, result.stderr || result.stdout);
}
async function withTempHome(fn) {
const home = await fs.mkdtemp(path.join(os.tmpdir(), "write-proxy-config-"));
try {
await fn(home);
} finally {
await fs.rm(home, { recursive: true, force: true });
}
}
async function readConfig(home) {
return await fs.readFile(path.join(home, "config.toml"), "utf8");
}
function countOccurrences(value, search) {
return value.split(search).length - 1;
}
test("creates config.toml when no config exists", async () => {
await withTempHome(async (home) => {
runWriteProxyConfig(home, 1234);
const config = await readConfig(home);
assert.match(
config,
new RegExp(`^${escapeRegExp(MANAGED_MODEL_PROVIDER_START)}\\n`)
);
assert.match(
config,
new RegExp(`\\n${escapeRegExp(MANAGED_PROXY_PROVIDER_END)}\\n$`)
);
assert.match(config, new RegExp(`model_provider = "${MODEL_PROVIDER}"`));
assert.match(config, /base_url = "http:\/\/127\.0\.0\.1:1234\/v1"/);
});
});
test("keeps one provider block when run twice", async () => {
await withTempHome(async (home) => {
runWriteProxyConfig(home, 1234);
runWriteProxyConfig(home, 5678);
const config = await readConfig(home);
assert.equal(countOccurrences(config, MANAGED_MODEL_PROVIDER_START), 1);
assert.equal(countOccurrences(config, MANAGED_MODEL_PROVIDER_END), 1);
assert.equal(countOccurrences(config, MANAGED_PROXY_PROVIDER_START), 1);
assert.equal(countOccurrences(config, MANAGED_PROXY_PROVIDER_END), 1);
assert.equal(
countOccurrences(config, `model_provider = "${MODEL_PROVIDER}"`),
1
);
assert.equal(
countOccurrences(config, `[model_providers.${MODEL_PROVIDER}]`),
1
);
});
});
test("updates the proxy port when run twice with a new port", async () => {
await withTempHome(async (home) => {
runWriteProxyConfig(home, 1234);
runWriteProxyConfig(home, 5678);
const config = await readConfig(home);
assert.doesNotMatch(config, /127\.0\.0\.1:1234/);
assert.match(config, /base_url = "http:\/\/127\.0\.0\.1:5678\/v1"/);
});
});
test("preserves unrelated config when writing proxy config", async () => {
await withTempHome(async (home) => {
await fs.mkdir(home, { recursive: true });
await fs.writeFile(
path.join(home, "config.toml"),
`model = "gpt-5"
approval_policy = "never"
[profiles.ci]
model = "gpt-5"
`,
"utf8"
);
runWriteProxyConfig(home, 1234);
const config = await readConfig(home);
const rootConfigIndex = config.indexOf('model = "gpt-5"');
const providerTableIndex = config.indexOf(
`[model_providers.${MODEL_PROVIDER}]`
);
assert.match(config, /^# BEGIN action-managed model provider config/);
assert.match(
config,
/model = "gpt-5"\napproval_policy = "never"\n\n\[profiles\.ci\]\nmodel = "gpt-5"/
);
assert.ok(rootConfigIndex > -1);
assert.ok(providerTableIndex > rootConfigIndex);
assert.equal(countOccurrences(config, `[profiles.ci]`), 1);
assert.equal(
countOccurrences(config, `[model_providers.${MODEL_PROVIDER}]`),
1
);
});
});
test("migrates old managed format when existing config used old comments", async () => {
await withTempHome(async (home) => {
await fs.mkdir(home, { recursive: true });
await fs.writeFile(
path.join(home, "config.toml"),
`# Added by codex-action.
model_provider = "${MODEL_PROVIDER}"
# Added by codex-action.
model_provider = "${MODEL_PROVIDER}"
[profiles.ci]
model = "gpt-5"
approval_policy = "never"
# Added by codex-action.
[model_providers.${MODEL_PROVIDER}]
name = "Codex Action Responses Proxy"
base_url = "http://127.0.0.1:1111/v1"
wire_api = "responses"
# Added by codex-action.
[model_providers.${MODEL_PROVIDER}]
name = "Codex Action Responses Proxy"
base_url = "http://127.0.0.1:2222/v1"
wire_api = "responses"
`,
"utf8"
);
runWriteProxyConfig(home, 3333);
const config = await readConfig(home);
assert.doesNotMatch(config, /# Added by codex-action\./);
assert.equal(countOccurrences(config, MANAGED_MODEL_PROVIDER_START), 1);
assert.equal(countOccurrences(config, MANAGED_PROXY_PROVIDER_START), 1);
assert.equal(
countOccurrences(config, `model_provider = "${MODEL_PROVIDER}"`),
1
);
assert.equal(
countOccurrences(config, `[model_providers.${MODEL_PROVIDER}]`),
1
);
assert.match(
config,
/\[profiles\.ci\]\nmodel = "gpt-5"\napproval_policy = "never"/
);
assert.ok(
config.indexOf("[profiles.ci]") <
config.indexOf(`[model_providers.${MODEL_PROVIDER}]`)
);
assert.match(config, /base_url = "http:\/\/127\.0\.0\.1:3333\/v1"/);
});
});
test("migrates compact old managed format with CRLF line endings", async () => {
await withTempHome(async (home) => {
await fs.mkdir(home, { recursive: true });
const config = [
"# Added by codex-action.",
`model_provider = "${MODEL_PROVIDER}"`,
"# Added by codex-action.",
`model_provider = "${MODEL_PROVIDER}"`,
"[profiles.ci]",
'model = "gpt-5"',
"# Added by codex-action.",
`[model_providers.${MODEL_PROVIDER}]`,
'name = "Codex Action Responses Proxy"',
'base_url = "http://127.0.0.1:1111/v1"',
'wire_api = "responses"',
].join("\r\n");
await fs.writeFile(path.join(home, "config.toml"), config, "utf8");
runWriteProxyConfig(home, 3333);
const updatedConfig = await readConfig(home);
assert.doesNotMatch(updatedConfig, /# Added by codex-action\./);
assert.equal(
countOccurrences(updatedConfig, `model_provider = "${MODEL_PROVIDER}"`),
1
);
assert.equal(
countOccurrences(updatedConfig, `[model_providers.${MODEL_PROVIDER}]`),
1
);
assert.match(updatedConfig, /\[profiles\.ci\]\r?\nmodel = "gpt-5"/);
assert.match(updatedConfig, /base_url = "http:\/\/127\.0\.0\.1:3333\/v1"/);
});
});
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}