Files
OmniRoute/tests/unit/combo-forecast.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

214 lines
6.8 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";
import { makeManagementSessionRequest } from "../helpers/managementSession.ts";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-forecast-"));
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
const ORIGINAL_INITIAL_PASSWORD = process.env.INITIAL_PASSWORD;
const ORIGINAL_JWT_SECRET = process.env.JWT_SECRET;
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const combosDb = await import("../../src/lib/db/combos.ts");
const settingsDb = await import("../../src/lib/db/settings.ts");
const quotaSnapshotsDb = await import("../../src/lib/db/quotaSnapshots.ts");
const callLogs = await import("../../src/lib/usage/callLogs.ts");
const comboForecast = await import("../../src/lib/usage/comboForecast.ts");
const route = await import("../../src/app/api/usage/combo-forecast/route.ts");
const { normalizeComboStep } = await import("../../src/lib/combos/steps.ts");
async function resetStorage() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
async function enableManagementAuth() {
process.env.INITIAL_PASSWORD = "combo-forecast-password";
await settingsDb.updateSettings({ requireLogin: true, password: "" });
}
async function seedPricing() {
await settingsDb.updatePricing({
openai: {
"gpt-4o-mini": {
input: 1,
output: 2,
cached: 0.5,
cache_creation: 1,
reasoning: 2,
},
},
});
}
async function seedForecastCombo() {
const comboInput = {
name: "combo-forecast-structured",
strategy: "weighted",
models: [
{
kind: "model",
providerId: "openai",
model: "openai/gpt-4o-mini",
connectionId: "forecast-conn-a",
label: "Forecast A",
},
{
kind: "model",
providerId: "openai",
model: "openai/gpt-4o-mini",
connectionId: "forecast-conn-b",
label: "Forecast B",
},
],
};
const combo = await combosDb.createCombo(comboInput);
const firstStep = normalizeComboStep(comboInput.models[0], {
comboName: comboInput.name,
index: 0,
});
const secondStep = normalizeComboStep(comboInput.models[1], {
comboName: comboInput.name,
index: 1,
});
return { combo, comboInput, firstStep, secondStep };
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(async () => {
await resetStorage();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
if (ORIGINAL_INITIAL_PASSWORD === undefined) delete process.env.INITIAL_PASSWORD;
else process.env.INITIAL_PASSWORD = ORIGINAL_INITIAL_PASSWORD;
if (ORIGINAL_JWT_SECRET === undefined) delete process.env.JWT_SECRET;
else process.env.JWT_SECRET = ORIGINAL_JWT_SECRET;
});
test("combo forecast projects cost and quota risk from combo history", async () => {
await seedPricing();
const { combo, comboInput, firstStep, secondStep } = await seedForecastCombo();
const timestamp = new Date(Date.now() - 60 * 60 * 1000).toISOString();
await callLogs.saveCallLog({
id: "combo-forecast-1",
timestamp,
status: 200,
model: "openai/gpt-4o-mini",
requestedModel: comboInput.name,
provider: "openai",
connectionId: "forecast-conn-a",
tokens: { prompt_tokens: 1_000, completion_tokens: 500 },
comboName: comboInput.name,
comboStepId: firstStep.id,
comboExecutionKey: firstStep.id,
});
await callLogs.saveCallLog({
id: "combo-forecast-2",
timestamp,
status: 200,
model: "openai/gpt-4o-mini",
requestedModel: comboInput.name,
provider: "openai",
connectionId: "forecast-conn-b",
tokens: { prompt_tokens: 2_000, completion_tokens: 1_000 },
comboName: comboInput.name,
comboStepId: secondStep.id,
comboExecutionKey: secondStep.id,
});
quotaSnapshotsDb.saveQuotaSnapshot({
provider: "openai",
connection_id: "forecast-conn-a",
window_key: "daily",
remaining_percentage: 90,
is_exhausted: 0,
next_reset_at: null,
window_duration_ms: 86_400_000,
raw_data: null,
});
quotaSnapshotsDb.saveQuotaSnapshot({
provider: "openai",
connection_id: "forecast-conn-a",
window_key: "daily",
remaining_percentage: 20,
is_exhausted: 0,
next_reset_at: null,
window_duration_ms: 86_400_000,
raw_data: null,
});
const forecast = await comboForecast.buildComboForecastResponse({
range: "7d",
horizon: "7d",
comboId: String(combo.id),
});
assert.equal(forecast.combos.length, 1);
assert.equal(forecast.combos[0].history.requests, 2);
assert.equal(forecast.combos[0].forecast.projectedRequests, 2);
assert.ok(forecast.combos[0].history.costUsd > 0);
assert.equal(forecast.combos[0].dataQuality.pricingCoveragePct, 100);
assert.equal(forecast.combos[0].targets.length, 2);
assert.equal(forecast.combos[0].targets[0].quota.scope, "connection");
assert.notEqual(forecast.combos[0].quotaRisk.level, "unknown");
});
test("combo forecast returns no_data confidence for combos without history", async () => {
const { combo } = await seedForecastCombo();
const forecast = await comboForecast.buildComboForecastResponse({
range: "24h",
horizon: "7d",
comboId: String(combo.id),
});
assert.equal(forecast.combos.length, 1);
assert.equal(forecast.combos[0].confidence, "no_data");
assert.equal(forecast.combos[0].history.requests, 0);
assert.equal(forecast.combos[0].forecast.projectedCostUsd, 0);
});
test("combo forecast API requires management auth and validates query", async () => {
await enableManagementAuth();
const { combo } = await seedForecastCombo();
const unauthenticated = await route.GET(
new Request(`http://localhost/api/usage/combo-forecast?comboId=${combo.id}`)
);
assert.equal(unauthenticated.status, 401);
const invalid = await route.GET(
await makeManagementSessionRequest("http://localhost/api/usage/combo-forecast?range=bad")
);
assert.equal(invalid.status, 400);
const missing = await route.GET(
await makeManagementSessionRequest(
"http://localhost/api/usage/combo-forecast?comboId=11111111-1111-4111-8111-111111111111"
)
);
assert.equal(missing.status, 404);
const authenticated = await route.GET(
await makeManagementSessionRequest(
`http://localhost/api/usage/combo-forecast?range=24h&horizon=7d&comboId=${combo.id}`
)
);
assert.equal(authenticated.status, 200);
const body = await authenticated.json();
assert.equal(body.combos.length, 1);
});