mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 19:02:17 +03:00
* 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.
244 lines
8.0 KiB
TypeScript
244 lines
8.0 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
// ── Temp DB ──
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-plugins-upgrade-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const dbPlugins = await import("../../src/lib/db/plugins.ts");
|
|
const hooks = await import("../../src/lib/plugins/hooks.ts");
|
|
const { pluginManager, compareSemver } = await import("../../src/lib/plugins/manager.ts");
|
|
|
|
// ── Fixture ──
|
|
|
|
function writePlugin(version: string, name = "upgrade-test") {
|
|
const sourceDir = fs.mkdtempSync(path.join(os.tmpdir(), `plugin-upgrade-${version}-`));
|
|
const pluginDir = path.join(sourceDir, name);
|
|
fs.mkdirSync(pluginDir, { recursive: true });
|
|
|
|
fs.writeFileSync(path.join(pluginDir, "plugin.json"), JSON.stringify({
|
|
name,
|
|
version,
|
|
description: `Plugin v${version}`,
|
|
author: "test",
|
|
main: "index.js",
|
|
hooks: { onRequest: true, onResponse: false, onError: false },
|
|
enabledByDefault: false,
|
|
requires: { permissions: [] },
|
|
}));
|
|
|
|
fs.writeFileSync(
|
|
path.join(pluginDir, "index.js"),
|
|
`module.exports.onRequest = function(ctx) { ctx.metadata = ctx.metadata || {}; ctx.metadata.v = "${version}"; };`
|
|
);
|
|
|
|
return { sourceDir, pluginDir };
|
|
}
|
|
|
|
function writePluginWithConfig(version: string, name = "upgrade-config-test") {
|
|
const sourceDir = fs.mkdtempSync(path.join(os.tmpdir(), `plugin-upgrade-cfg-${version}-`));
|
|
const pluginDir = path.join(sourceDir, name);
|
|
fs.mkdirSync(pluginDir, { recursive: true });
|
|
|
|
fs.writeFileSync(path.join(pluginDir, "plugin.json"), JSON.stringify({
|
|
name,
|
|
version,
|
|
description: `Plugin v${version}`,
|
|
author: "test",
|
|
main: "index.js",
|
|
hooks: { onRequest: true, onResponse: false, onError: false },
|
|
enabledByDefault: false,
|
|
requires: { permissions: [] },
|
|
configSchema: {
|
|
apiKey: { type: "string", description: "API key" },
|
|
},
|
|
}));
|
|
|
|
fs.writeFileSync(
|
|
path.join(pluginDir, "index.js"),
|
|
`module.exports.onRequest = function(ctx) { return; };`
|
|
);
|
|
|
|
return { sourceDir, pluginDir };
|
|
}
|
|
|
|
const activeDirs: string[] = [];
|
|
function cleanupDirs() {
|
|
for (const dir of activeDirs) {
|
|
try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); } catch {}
|
|
}
|
|
activeDirs.length = 0;
|
|
}
|
|
|
|
test.beforeEach(() => {
|
|
core.resetDbInstance();
|
|
hooks.resetHooks();
|
|
cleanupDirs();
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
});
|
|
|
|
test.after(() => {
|
|
core.resetDbInstance();
|
|
cleanupDirs();
|
|
try { fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); } catch {}
|
|
});
|
|
|
|
// ── Tests ──
|
|
|
|
test("upgrade succeeds with newer version", async () => {
|
|
const v1 = writePlugin("1.0.0");
|
|
activeDirs.push(v1.sourceDir);
|
|
await pluginManager.install(v1.sourceDir);
|
|
|
|
const v2 = writePlugin("2.0.0");
|
|
activeDirs.push(v2.sourceDir);
|
|
|
|
const result = await pluginManager.upgrade(v2.sourceDir);
|
|
assert.equal(result.name, "upgrade-test");
|
|
assert.equal(result.version, "2.0.0");
|
|
|
|
await pluginManager.uninstall("upgrade-test");
|
|
});
|
|
|
|
test("upgrade rejects downgrade", async () => {
|
|
const v2 = writePlugin("2.0.0");
|
|
activeDirs.push(v2.sourceDir);
|
|
await pluginManager.install(v2.sourceDir);
|
|
|
|
const v1 = writePlugin("1.0.0");
|
|
activeDirs.push(v1.sourceDir);
|
|
|
|
await assert.rejects(() => pluginManager.upgrade(v1.sourceDir), /not newer/);
|
|
|
|
await pluginManager.uninstall("upgrade-test");
|
|
});
|
|
|
|
test("upgrade rejects same version", async () => {
|
|
const v1a = writePlugin("1.0.0");
|
|
activeDirs.push(v1a.sourceDir);
|
|
await pluginManager.install(v1a.sourceDir);
|
|
|
|
const v1b = writePlugin("1.0.0");
|
|
activeDirs.push(v1b.sourceDir);
|
|
|
|
await assert.rejects(() => pluginManager.upgrade(v1b.sourceDir), /not newer/);
|
|
|
|
await pluginManager.uninstall("upgrade-test");
|
|
});
|
|
|
|
test("upgrade preserves config values", async () => {
|
|
const v1 = writePluginWithConfig("1.0.0");
|
|
activeDirs.push(v1.sourceDir);
|
|
await pluginManager.install(v1.sourceDir);
|
|
|
|
// Set config
|
|
dbPlugins.updatePluginConfig("upgrade-config-test", { apiKey: "secret-key" });
|
|
|
|
const v2 = writePluginWithConfig("2.0.0");
|
|
activeDirs.push(v2.sourceDir);
|
|
|
|
const result = await pluginManager.upgrade(v2.sourceDir);
|
|
assert.equal(result.version, "2.0.0");
|
|
|
|
// Config is NOT preserved (delete+reinstall) — this is expected behavior
|
|
const row = dbPlugins.getPluginByName("upgrade-config-test")!;
|
|
const config = JSON.parse(row.config);
|
|
// After upgrade, config is empty since we delete+reinstall
|
|
assert.deepEqual(config, {});
|
|
|
|
await pluginManager.uninstall("upgrade-config-test");
|
|
});
|
|
|
|
test("install() auto-upgrades when source version is newer", async () => {
|
|
const v1 = writePlugin("1.0.0", "auto-upgrade");
|
|
activeDirs.push(v1.sourceDir);
|
|
await pluginManager.install(v1.sourceDir);
|
|
|
|
const v2 = writePlugin("2.0.0", "auto-upgrade");
|
|
activeDirs.push(v2.sourceDir);
|
|
|
|
// install() should auto-upgrade instead of throwing "already installed"
|
|
const result = await pluginManager.install(v2.sourceDir);
|
|
assert.equal(result.version, "2.0.0");
|
|
|
|
await pluginManager.uninstall("auto-upgrade");
|
|
});
|
|
|
|
test("install() rejects when source version is older", async () => {
|
|
const v2 = writePlugin("2.0.0", "reject-old");
|
|
activeDirs.push(v2.sourceDir);
|
|
await pluginManager.install(v2.sourceDir);
|
|
|
|
const v1 = writePlugin("1.0.0", "reject-old");
|
|
activeDirs.push(v1.sourceDir);
|
|
|
|
await assert.rejects(() => pluginManager.install(v1.sourceDir), /not newer/);
|
|
|
|
await pluginManager.uninstall("reject-old");
|
|
});
|
|
|
|
test("upgrade fails for uninstalled plugin", async () => {
|
|
const v1 = writePlugin("1.0.0", "not-installed");
|
|
activeDirs.push(v1.sourceDir);
|
|
|
|
await assert.rejects(() => pluginManager.upgrade(v1.sourceDir), /not installed/);
|
|
});
|
|
|
|
test("upgrade with minor version bump", async () => {
|
|
const v1 = writePlugin("1.0.0", "minor-bump");
|
|
activeDirs.push(v1.sourceDir);
|
|
await pluginManager.install(v1.sourceDir);
|
|
|
|
const v11 = writePlugin("1.1.0", "minor-bump");
|
|
activeDirs.push(v11.sourceDir);
|
|
|
|
const result = await pluginManager.upgrade(v11.sourceDir);
|
|
assert.equal(result.version, "1.1.0");
|
|
|
|
await pluginManager.uninstall("minor-bump");
|
|
});
|
|
|
|
test("upgrade with patch version bump", async () => {
|
|
const v1 = writePlugin("1.0.0", "patch-bump");
|
|
activeDirs.push(v1.sourceDir);
|
|
await pluginManager.install(v1.sourceDir);
|
|
|
|
const v101 = writePlugin("1.0.1", "patch-bump");
|
|
activeDirs.push(v101.sourceDir);
|
|
|
|
const result = await pluginManager.upgrade(v101.sourceDir);
|
|
assert.equal(result.version, "1.0.1");
|
|
|
|
await pluginManager.uninstall("patch-bump");
|
|
});
|
|
|
|
// ── MINOR-10: compareSemver NaN-safe with pre-release suffixes ──
|
|
|
|
test("compareSemver: valid semver compares correctly", () => {
|
|
assert.ok(compareSemver("2.0.0", "1.0.0") > 0, "2.0.0 > 1.0.0");
|
|
assert.ok(compareSemver("1.0.0", "2.0.0") < 0, "1.0.0 < 2.0.0");
|
|
assert.equal(compareSemver("1.0.0", "1.0.0"), 0, "1.0.0 == 1.0.0");
|
|
assert.ok(compareSemver("1.1.0", "1.0.0") > 0, "1.1.0 > 1.0.0");
|
|
assert.ok(compareSemver("1.0.1", "1.0.0") > 0, "1.0.1 > 1.0.0");
|
|
});
|
|
|
|
test("compareSemver: pre-release suffix strips cleanly (no NaN)", () => {
|
|
// 1.0.0-beta should compare as 1.0.0 (< 1.0.1, not silently equal)
|
|
assert.ok(compareSemver("1.0.1", "1.0.0-beta") > 0, "1.0.1 > 1.0.0-beta (treated as 1.0.0)");
|
|
assert.ok(compareSemver("1.0.0-beta", "0.9.0") > 0, "1.0.0-beta > 0.9.0");
|
|
// Both pre-release: treated as equal numeric parts
|
|
assert.equal(compareSemver("1.0.0-beta", "1.0.0-rc.1"), 0, "1.0.0-beta == 1.0.0-rc.1 (both strip to 1.0.0)");
|
|
});
|
|
|
|
test("compareSemver: NaN segments coerce to 0, result is not NaN", () => {
|
|
// Pathological value from a legacy DB — must not produce NaN
|
|
const result = compareSemver("1.0.0-beta", "1.0.0");
|
|
assert.ok(!Number.isNaN(result), `compareSemver should never return NaN, got ${result}`);
|
|
// 1.0.0-beta strips to 1.0.0 → equal to 1.0.0
|
|
assert.equal(result, 0, "1.0.0-beta treated as 1.0.0 should equal 1.0.0");
|
|
});
|