Files
OmniRoute/tests/unit/docker-base-path-patch.test.ts
Rouzbeh† e3bca29bbc fix(docker): real image tags (bifrost/cliproxyapi) + complete OMNIROUTE_BASE_PATH runtime patcher (#10482)
* fix(docker): real image tags + complete OMNIROUTE_BASE_PATH runtime patcher

Three docker issues fixed:

1. Images that do not exist:
   - bifrost: ghcr.io/maximhq/bifrost:1.5.21 never existed (1.5.x tops at
     v1.5.16, all tags carry the v prefix) -> ghcr.io/maximhq/bifrost:v1.6.11
   - cliproxyapi: ghcr.io/router-for-me/* is not publicly pullable (403);
     the official prebuilt image is docker.io/eceasy/cli-proxy-api, where
     the pinned v6.9.7 exists -> docker.io/eceasy/cli-proxy-api:v6.9.7
   - Verified still-current: redis:8.6.5-alpine (already on Redis 8 since
     #9065; ioredis 5.10 is RESP2/3-compatible, no modules used) and
     qdrant:v1.12.4 -- both exist, unchanged.

2. OMNIROUTE_BASE_PATH ignored on prebuilt images (root cause):
   Next 16 (webpack and Turbopack) app-router renders SSR asset URLs from
   assetPrefix ALONE; basePath only affects routing. The runtime patcher
   (ensure-docker-base-path) rewrote basePath literals only, so a prebuilt
   root-path image patched to /omniroute served the page but every
   /_next/static shell reference stayed unprefixed (404 behind a subpath
   proxy), the RSC flight-payload chunk refs came from client-reference
   manifests baked with unprefixed paths, and the Turbopack client process
   shim ships an empty env object so the client never learns the subpath.
   Extended patch-standalone-base-path.mjs to also rewrite:
   - assetPrefix literals (mirrors the subpath for SSR asset URLs)
   - the NEXT_PUBLIC_OMNIROUTE_BASE_PATH env mirror in the inline config
   - the client process.env shim (.env={}) with the two basePath keys
   - every baked "/_next/static URL (manifests, media imports, .html pages)
   next.config.mjs now mirrors basePath into assetPrefix so REBUILT images
   bake prefixed assets too. E2E-verified on the published main-web image:
   HTML under /omniroute now has 16/16 prefixed JS srcs and 82/82 prefixed
   flight refs (was 13/9 + ~150 unprefixed), prefixed assets return 200.

* chore(changelog): fragment for #10482 (docker images + basepath patcher)

* chore(changelog): bullet-form fragment for #10482

* Merge branch 'release/v3.8.50' into fix/docker-compose-images-and-basepath

* test(fix): refresh expired alibaba quota sample validity and onnxruntime pin for v3.8.50 base

- alibaba-free-tier-quota-fetcher.test.ts: sample quotaValidityPeriod
  (2026-08-16 16:00 UTC) is in the past, making every quota entry classify
  as expired/not_capable; bump to 2028-01-01 UTC so the text/merge
  classification tests exercise the intended path again.
- optional-transformers-dependency.test.ts: onnxruntime-node pin assertion
  updated from ~1.24.3 to ~1.27.0 to match package.json (bumped by #10403);
  the regular-not-optional intent is unchanged.

---------

Co-authored-by: Rouzbeh <rqzbeh@users.noreply.github.com>
2026-08-17 08:24:27 -03:00

117 lines
4.5 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 {
patchBasePathLiterals,
patchJsonManifestFile,
patchStandaloneBasePath,
patchProcessEnvShim,
patchBakedAssetUrls,
} from "../../scripts/docker/patch-standalone-base-path.mjs";
test("patchBasePathLiterals rewrites empty basePath literals", () => {
const input = 'const cfg={basePath:"",assetPrefix:void 0};"basePath":""';
const output = patchBasePathLiterals(input, "/omniroute");
assert.match(output, /basePath:"\/omniroute"/);
assert.match(output, /"basePath":"\/omniroute"/);
});
test("patchJsonManifestFile updates nested basePath fields", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-basepath-"));
const filePath = path.join(dir, "routes-manifest.json");
fs.writeFileSync(filePath, JSON.stringify({ basePath: "", nested: { basePath: "" } }, null, 2));
assert.equal(patchJsonManifestFile(filePath, "/omniroute"), true);
const parsed = JSON.parse(fs.readFileSync(filePath, "utf8"));
assert.equal(parsed.basePath, "/omniroute");
assert.equal(parsed.nested.basePath, "/omniroute");
});
test("patchStandaloneBasePath rewrites a root-path standalone tree", () => {
const appRoot = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-standalone-"));
const distRoot = path.join(appRoot, ".build", "next");
fs.mkdirSync(path.join(distRoot, "server"), { recursive: true });
fs.writeFileSync(path.join(distRoot, "routes-manifest.json"), JSON.stringify({ basePath: "" }));
fs.writeFileSync(path.join(distRoot, "server", "chunk.js"), 'export const config={basePath:""};');
fs.writeFileSync(path.join(appRoot, "BUILD_OMNIROUTE_BASE_PATH"), "\n");
const result = patchStandaloneBasePath({
appRoot,
fromBasePath: "",
toBasePath: "/omniroute",
});
assert.equal(result.changed, true);
assert.equal(
JSON.parse(fs.readFileSync(path.join(distRoot, "routes-manifest.json"), "utf8")).basePath,
"/omniroute"
);
assert.match(fs.readFileSync(path.join(distRoot, "server", "chunk.js"), "utf8"), /\/omniroute/);
});
test("patchStandaloneBasePath rejects mismatched non-root builds", () => {
assert.throws(
() =>
patchStandaloneBasePath({
appRoot: process.cwd(),
fromBasePath: "/custom",
toBasePath: "/omniroute",
}),
/does not match the image build/
);
});
test("patchBasePathLiterals rewrites assetPrefix literals (Next 16 SSR asset URLs)", () => {
// Next 16 app-router renders SSR asset URLs from assetPrefix ALONE.
assert.equal(
patchBasePathLiterals('{"assetPrefix":""}', "/omniroute"),
'{"assetPrefix":"/omniroute"}'
);
assert.equal(patchBasePathLiterals('assetPrefix:""', "/omniroute"), 'assetPrefix:"/omniroute"');
assert.equal(
patchBasePathLiterals("assetPrefix:void 0", "/omniroute"),
'assetPrefix:"/omniroute"'
);
// Asset prefix must mirror the basePath so both routing and assets align.
const mixed = patchBasePathLiterals('{"basePath":"","assetPrefix":""}', "/omniroute");
assert.match(mixed, /"basePath":"\/omniroute"/);
assert.match(mixed, /"assetPrefix":"\/omniroute"/);
});
test("patchBasePathLiterals rewrites the NEXT_PUBLIC env mirror", () => {
assert.equal(
patchBasePathLiterals('{"env":{"NEXT_PUBLIC_OMNIROUTE_BASE_PATH":""}}', "/omniroute"),
'{"env":{"NEXT_PUBLIC_OMNIROUTE_BASE_PATH":"/omniroute"}}'
);
assert.equal(
patchBasePathLiterals('NEXT_PUBLIC_OMNIROUTE_BASE_PATH:""', "/omniroute"),
'NEXT_PUBLIC_OMNIROUTE_BASE_PATH:"/omniroute"'
);
});
test("patchProcessEnvShim populates the Turbopack client process env", () => {
assert.equal(
patchProcessEnvShim("o.env={},o.argv=[]", "/omniroute"),
'o.env={OMNIROUTE_BASE_PATH:"/omniroute",NEXT_PUBLIC_OMNIROUTE_BASE_PATH:"/omniroute"},o.argv=[]'
);
// Non-empty env objects are left untouched (never clobber baked values).
assert.equal(patchProcessEnvShim("o.env={A:1}", "/omniroute"), "o.env={A:1}");
});
test("patchBakedAssetUrls prefixes absolute _next/static URLs", () => {
assert.equal(
patchBakedAssetUrls('"/_next/static/chunks/a.js"', "/omniroute"),
'"/omniroute/_next/static/chunks/a.js"'
);
assert.equal(
patchBakedAssetUrls("'/_next/static/media/m.png'", "/omniroute"),
"'/omniroute/_next/static/media/m.png'"
);
// Already-prefixed URLs are stable.
assert.equal(
patchBakedAssetUrls('"/omniroute/_next/static/a.js"', "/omniroute"),
'"/omniroute/_next/static/a.js"'
);
});