Files
OmniRoute/tests/integration/opencode-config-startup.test.ts
Diego Rodrigues de Sa e Souza 93265eede3 test(infra): retry recursive temp-dir removal on main (main twin of #11968) (#12246)
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)

`main` has been red since b342c1a361 on the vitest and integration gates:

  ✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
  ✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos

Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).

release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.

This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.

The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.

* chore(scripts): carry the rm-maxretries codemod onto main alongside its output

The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
2026-09-01 01:48:00 -03:00

115 lines
4.5 KiB
TypeScript

import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { createRequire } from "node:module";
import { after, it } from "node:test";
const require = createRequire(import.meta.url);
// Read the pin from the installed package instead of hard-coding it: the constant
// was frozen at 1.18.8 and silently went stale when Dependabot bumped opencode-ai
// to 1.18.18 (#10626), turning this into a base-red. Sourcing it from the resolved
// package.json keeps the assertion just as strict (the binary must report exactly
// the version this repo pins) while surviving future bumps.
const OPENCODE_VERSION: string = require("opencode-ai/package.json").version;
const testHome = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-opencode-8849-"));
const originalHome = process.env.HOME;
const originalFetch = globalThis.fetch;
process.env.HOME = testHome;
after(() => {
globalThis.fetch = originalFetch;
if (originalHome === undefined) delete process.env.HOME;
else process.env.HOME = originalHome;
fs.rmSync(testHome, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
function runOpencode(binary: string, args: string[]) {
const xdgRoot = path.join(testHome, "xdg");
const result = spawnSync(binary, args, {
cwd: testHome,
encoding: "utf8",
timeout: 30_000,
env: {
...process.env,
HOME: testHome,
XDG_CONFIG_HOME: path.join(xdgRoot, "config"),
XDG_DATA_HOME: path.join(xdgRoot, "data"),
XDG_CACHE_HOME: path.join(xdgRoot, "cache"),
XDG_STATE_HOME: path.join(xdgRoot, "state"),
NO_COLOR: "1",
OPENCODE_DISABLE_AUTOUPDATE: "1",
},
});
assert.ifError(result.error);
return result;
}
it("#8849 generated config is accepted by pinned OpenCode schema and startup", async () => {
const packageJsonPath = require.resolve("opencode-ai/package.json");
const opencodeBinary = path.join(path.dirname(packageJsonPath), "bin", "opencode.exe");
assert.ok(fs.existsSync(opencodeBinary), `missing pinned OpenCode ${OPENCODE_VERSION} binary`);
const version = runOpencode(opencodeBinary, ["--version"]);
assert.strictEqual(version.status, 0, version.stderr);
assert.strictEqual(version.stdout.trim(), OPENCODE_VERSION);
const catalog = {
object: "list",
data: [
{ id: "context-only", context_length: 131072 },
{ id: "context-input", context_length: 131072, max_input_tokens: 100000 },
{
id: "context-input-output",
context_length: 131072,
max_input_tokens: 100000,
max_output_tokens: 32768,
},
{ id: "no-limit-metadata" },
],
};
globalThis.fetch = (async () =>
new Response(JSON.stringify(catalog), {
status: 200,
headers: { "content-type": "application/json" },
})) as typeof fetch;
const { generateOpencodeConfig } =
await import("../../src/lib/cli-helper/config-generator/opencode.ts");
const generatedConfig = await generateOpencodeConfig({
baseUrl: "http://127.0.0.1:9/v1",
apiKey: "sk-test",
providerId: "issue8849",
});
const configDir = path.join(testHome, "xdg", "config", "opencode");
fs.mkdirSync(configDir, { recursive: true });
fs.writeFileSync(path.join(configDir, "opencode.json"), generatedConfig);
const configCheck = runOpencode(opencodeBinary, ["debug", "config", "--pure"]);
assert.strictEqual(configCheck.status, 0, configCheck.stderr);
assert.doesNotMatch(configCheck.stderr, /Missing key .*\.limit\.output/);
const resolvedConfig = JSON.parse(configCheck.stdout);
assert.ok(resolvedConfig.provider.issue8849.models["context-only"].limit.output > 0);
assert.strictEqual(
resolvedConfig.provider.issue8849.models["context-input-output"].limit.output,
32768
);
// #11035/#11032 (PR #11054) made the generator ALWAYS emit both limit keys: a model
// the catalog knows nothing about now gets the 128K context / 8K output fallbacks,
// because OpenCode's v1 provider schema rejects the whole config on a missing
// `limit.context` / `limit.output`. Before that fix the entry carried no `limit` at all.
assert.deepStrictEqual(resolvedConfig.provider.issue8849.models["no-limit-metadata"].limit, {
context: 128_000,
output: 8_192,
});
const startup = runOpencode(opencodeBinary, ["debug", "startup", "--pure"]);
assert.strictEqual(startup.status, 0, startup.stderr);
assert.match(startup.stdout.trim(), /^\d+(?:\.\d+)?$/);
assert.doesNotMatch(startup.stderr, /Missing key .*\.limit\.output/);
});