Files
OmniRoute/tests/unit/live-ws-public-url.test.ts
Webman 50bc8ab8aa fix(barrel): delete the @/lib/localDb barrel — every consumer migrated (#11795 Phase 5) (#12055)
Resynced onto the release tip after #12051/#12052/#12053 landed. Same LKGP-clear conflict as #12053 (kept the current clearStaleLKGP() helper at both call sites). One additional issue this final phase's combined-worktree validation surfaced: clearStaleLKGP() itself (added by #12013, which none of the 4 phase PRs could have seen since it landed after they were authored) still had a dynamic `await import("@/lib/localDb")` — a real break once this PR deletes the barrel. Fixed to `await import("@/lib/db/settings")`, matching the direct-import pattern used at every other call site. typecheck:core, check-db-rules, check:cycles, and the eslint-import-boundaries regression test (3/3, including "G14 rejects localDb barrel imports") all green after resync — zero barrel-importing production files remain. Nice clean 5-phase migration, and thanks for taking on the full #11795 cleanup.
2026-08-30 02:36:26 -03:00

156 lines
5.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";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-live-ws-public-url-"));
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
const ORIGINAL_API_KEY_SECRET = process.env.API_KEY_SECRET;
const ORIGINAL_PUBLIC_URL = process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL;
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "test-live-ws-public-url-secret";
const core = await import("../../src/lib/db/core.ts");
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
const { updateSettings } = await import("@/lib/db/settings");
const localDb = { updateSettings };
const wsRoute = await import("../../src/app/api/v1/ws/route.ts");
function resetStorage() {
apiKeysDb.resetApiKeyState();
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(async () => {
resetStorage();
delete process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL;
await localDb.updateSettings({
wsAuth: false,
requireLogin: true,
password: "hashed-password",
});
});
test.after(() => {
apiKeysDb.resetApiKeyState();
core.resetDbInstance();
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_API_KEY_SECRET === undefined) {
delete process.env.API_KEY_SECRET;
} else {
process.env.API_KEY_SECRET = ORIGINAL_API_KEY_SECRET;
}
if (ORIGINAL_PUBLIC_URL === undefined) {
delete process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL;
} else {
process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL = ORIGINAL_PUBLIC_URL;
}
});
test("handshake response includes publicUrl when NEXT_PUBLIC_LIVE_WS_PUBLIC_URL is set", async () => {
process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL = "wss://ws.my-ai.com/live-ws";
const response = await wsRoute.GET(
new Request("http://localhost/api/v1/ws?handshake=1", {
headers: { origin: "http://localhost" },
})
);
assert.equal(response.status, 200);
const body = await response.json();
assert.equal(body.live.publicUrl, "wss://ws.my-ai.com/live-ws");
});
test("handshake response includes null publicUrl when NEXT_PUBLIC_LIVE_WS_PUBLIC_URL is unset", async () => {
delete process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL;
const response = await wsRoute.GET(
new Request("http://localhost/api/v1/ws?handshake=1", {
headers: { origin: "http://localhost" },
})
);
assert.equal(response.status, 200);
const body = await response.json();
assert.equal(body.live.publicUrl, null);
});
test("protocol.live.publicUrl reflects env set after module import (lazy read)", async () => {
process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL = "wss://custom.example.com/ws";
const response = await wsRoute.GET(new Request("http://localhost/api/v1/ws"));
assert.equal(response.status, 426);
const body = await response.json();
assert.equal(body.protocol.live.publicUrl, "wss://custom.example.com/ws");
});
test("publicUrl with non-WebSocket scheme is rejected (null)", async () => {
process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL = "https://ws.my-ai.com/live-ws";
const response = await wsRoute.GET(
new Request("http://localhost/api/v1/ws?handshake=1", {
headers: { origin: "http://localhost" },
})
);
assert.equal(response.status, 200);
const body = await response.json();
assert.equal(body.live.publicUrl, null);
assert.equal(body.protocol.live.publicUrl, null);
});
test("publicUrl with ws:// scheme is accepted", async () => {
process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL = "ws://lan-host:20132/live-ws";
const response = await wsRoute.GET(
new Request("http://localhost/api/v1/ws?handshake=1", {
headers: { origin: "http://localhost" },
})
);
assert.equal(response.status, 200);
const body = await response.json();
assert.equal(body.live.publicUrl, "ws://lan-host:20132/live-ws");
});
test("handshake path is derived from NEXT_PUBLIC_LIVE_WS_PUBLIC_URL pathname", async () => {
process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL = "wss://ws.my-ai.com/my-custom-ws";
const response = await wsRoute.GET(
new Request("http://localhost/api/v1/ws?handshake=1", {
headers: { origin: "http://localhost" },
})
);
assert.equal(response.status, 200);
const body = await response.json();
assert.equal(body.live.path, "/my-custom-ws");
});
test("handshake path defaults to /live-ws when NEXT_PUBLIC_LIVE_WS_PUBLIC_URL is unset", async () => {
delete process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL;
const response = await wsRoute.GET(
new Request("http://localhost/api/v1/ws?handshake=1", {
headers: { origin: "http://localhost" },
})
);
assert.equal(response.status, 200);
const body = await response.json();
assert.equal(body.live.path, "/live-ws");
});