@@ -178,3 +178,61 @@ test('rejects a PAYG provider in a provider group named providers', () => { |
178 | 178 | assert.match(issues[0], /provider-route/); |
179 | 179 | assert.ok(issues[0].includes('$.experimental.provider_routes.providers.providers[0]')); |
180 | 180 | }); |
| 181 | + |
| 182 | +// #4694: the entry-point check must run main() when the guard is invoked |
| 183 | +// through a symlink (file or directory). Before the realpath fix both |
| 184 | +// invocations silently exited 0 without checking anything. |
| 185 | +import { spawnSync } from 'node:child_process'; |
| 186 | +import { mkdtempSync, symlinkSync, writeFileSync } from 'node:fs'; |
| 187 | +import { tmpdir } from 'node:os'; |
| 188 | +import { dirname, join } from 'node:path'; |
| 189 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 190 | + |
| 191 | +const guardPath = fileURLToPath(new URL('./check-opencode-metered-providers.mjs', import.meta.url)); |
| 192 | +const meteredConfig = '{ "model": "deepseek/deepseek-chat" }'; |
| 193 | +const cleanCliConfig = '{ "model": "openai/gpt-6" }'; |
| 194 | + |
| 195 | +function runGuard(scriptPath, configSource) { |
| 196 | + const dir = mkdtempSync(join(tmpdir(), 'metered-guard-')); |
| 197 | + const configPath = join(dir, 'config.jsonc'); |
| 198 | + writeFileSync(configPath, configSource); |
| 199 | + return spawnSync(process.execPath, [scriptPath, configPath], { encoding: 'utf8' }); |
| 200 | +} |
| 201 | + |
| 202 | +test('guard runs when invoked through a file symlink (metered config fails)', () => { |
| 203 | + const dir = mkdtempSync(join(tmpdir(), 'metered-guard-link-')); |
| 204 | + const linkPath = join(dir, 'guard-link.mjs'); |
| 205 | + symlinkSync(guardPath, linkPath); |
| 206 | + const res = runGuard(linkPath, meteredConfig); |
| 207 | + assert.equal(res.status, 1); |
| 208 | + assert.match(res.stderr, /Metered-provider config guard failed/); |
| 209 | + assert.match(res.stderr, /deepseek/); |
| 210 | +}); |
| 211 | + |
| 212 | +test('guard runs when invoked through a file symlink (clean config passes)', () => { |
| 213 | + const dir = mkdtempSync(join(tmpdir(), 'metered-guard-link-')); |
| 214 | + const linkPath = join(dir, 'guard-link.mjs'); |
| 215 | + symlinkSync(guardPath, linkPath); |
| 216 | + const res = runGuard(linkPath, cleanCliConfig); |
| 217 | + assert.equal(res.status, 0); |
| 218 | + assert.match(res.stdout, /Metered-provider config guard passed/); |
| 219 | +}); |
| 220 | + |
| 221 | +test('guard runs when invoked through a symlinked directory (metered config fails)', () => { |
| 222 | + const dir = mkdtempSync(join(tmpdir(), 'metered-guard-dirlink-')); |
| 223 | + const linkDir = join(dir, 'bin-link'); |
| 224 | + symlinkSync(dirname(guardPath), linkDir, 'dir'); |
| 225 | + const res = runGuard(join(linkDir, 'check-opencode-metered-providers.mjs'), meteredConfig); |
| 226 | + assert.equal(res.status, 1); |
| 227 | + assert.match(res.stderr, /Metered-provider config guard failed/); |
| 228 | +}); |
| 229 | + |
| 230 | +test('importing the guard module does not run main()', () => { |
| 231 | + const res = spawnSync( |
| 232 | + process.execPath, |
| 233 | + ['--input-type=module', '-e', `import ${JSON.stringify(pathToFileURL(guardPath).href)};`], |
| 234 | + { encoding: 'utf8' }, |
| 235 | + ); |
| 236 | + assert.equal(res.status, 0); |
| 237 | + assert.doesNotMatch(res.stdout + res.stderr, /Metered-provider config guard/); |
| 238 | +}); |