mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-20 22:02:19 +03:00
A documented byte estimate should never come back negative, even where today's usage makes it harmless — `Math.max(0, …)` is the honest floor. Replacing the three hardcoded `data:image/jpeg;base64,` literals with the shared constant is byte-identical and removes three chances to drift. --- Validated in one consolidated worktree cut from `release/v3.8.51`, boarded with the rest of this batch — zero conflicts between the 19 PRs. - `typecheck:core` clean; `check:dashboard-typecheck` OK (206 pre-existing, all within the frozen baseline); `check:changelog-integrity` OK - complexity 2802 / baseline 3218 and cognitive-complexity 1267 / baseline 1437 — both under baseline - 226 of 228 focused assertions green across the batch's 23 test files. The 2 remaining belong to #12551, which is held separately. Two batch-owned defects were found and fixed in flight, both pure base drift: `173_xp_action_counts.sql` collided with `173_call_logs_video_content_removed.sql` (renumbered to 176 on #12651 — it aborted every DB open, which is what 53 of the first run's failures were), and the feature-flag catalog was missing the `SERVER_OWNED_TOOL_LOOP_ENABLED` row the base gained after #12552 was written. ⚠️ base-red inherited: #12732 — `Docs Gates`, `Merge integrity`, `No new ESLint warnings`, `Unit Tests fast-path` and `Fast Quality Gates` reproduce on the pure tip (provider count 356 vs the 358 the modules define, SKILL.md drift, and `open-sse/utils/stream.ts` at 3115 > frozen 3098, which this batch does not touch). Thanks @pacocartones — the `file:line` citations and the explicit out-of-scope notes on every one of these made a 19-PR batch reviewable in one pass.
74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import {
|
|
JPEG_FRAME_DATA_URI_PREFIX,
|
|
decodeJpegFrameDataUri,
|
|
estimateJpegFrameBytes,
|
|
} from "../../../src/lib/guardrails/videoBridgeFrameContract";
|
|
|
|
test("decodes a valid JPEG data URI case-insensitively", () => {
|
|
const bytes = Buffer.from("abc");
|
|
const uri = `data:image/JPEG;base64,${bytes.toString("base64")}`;
|
|
assert.deepEqual(decodeJpegFrameDataUri(uri), bytes);
|
|
assert.equal(JPEG_FRAME_DATA_URI_PREFIX, "data:image/jpeg;base64,");
|
|
});
|
|
|
|
test("rejects non-JPEG and malformed URIs with a stable message", () => {
|
|
for (const bad of [
|
|
"data:image/png;base64,QQ==",
|
|
"data:image/jpeg;base64,@@invalid@@",
|
|
"data:image/jpeg,plain",
|
|
"https://example.com/frame.jpg",
|
|
"",
|
|
]) {
|
|
assert.throws(() => decodeJpegFrameDataUri(bad), /not a JPEG data URI/i);
|
|
assert.throws(() => estimateJpegFrameBytes(bad), /not a JPEG data URI/i);
|
|
}
|
|
});
|
|
|
|
test("estimates decoded bytes without decoding, accounting for padding", () => {
|
|
for (const source of ["a", "ab", "abc", "abcd", "x".repeat(3000)]) {
|
|
const uri = `data:image/jpeg;base64,${Buffer.from(source).toString("base64")}`;
|
|
assert.equal(estimateJpegFrameBytes(uri), Buffer.byteLength(source));
|
|
}
|
|
});
|
|
|
|
test("never estimates below zero for degenerate padding-only payloads (#12323)", () => {
|
|
// The charset-only pattern admits these; the estimate must clamp instead of going to -1.
|
|
for (const encoded of ["=", "==", "A=", "A=="]) {
|
|
const uri = `${JPEG_FRAME_DATA_URI_PREFIX}${encoded}`;
|
|
const estimate = estimateJpegFrameBytes(uri);
|
|
assert.ok(estimate >= 0, `${JSON.stringify(encoded)} estimated ${estimate}`);
|
|
assert.ok(
|
|
estimate >= decodeJpegFrameDataUri(uri).byteLength,
|
|
`${JSON.stringify(encoded)} estimate is not an upper bound`
|
|
);
|
|
}
|
|
assert.equal(estimateJpegFrameBytes(`${JPEG_FRAME_DATA_URI_PREFIX}=`), 0);
|
|
assert.equal(estimateJpegFrameBytes(`${JPEG_FRAME_DATA_URI_PREFIX}==`), 0);
|
|
});
|
|
|
|
test("encode sites build frame data URIs from JPEG_FRAME_DATA_URI_PREFIX (#12323)", () => {
|
|
const guardrailsDir = path.resolve(
|
|
path.dirname(fileURLToPath(import.meta.url)),
|
|
"../../../src/lib/guardrails"
|
|
);
|
|
for (const file of [
|
|
"videoBridgeContactSheet.ts",
|
|
"videoBridgeRuntime.ts",
|
|
"videoBridgeDrilldownLifecycle.ts",
|
|
]) {
|
|
const source = fs.readFileSync(path.join(guardrailsDir, file), "utf8");
|
|
assert.doesNotMatch(source, /data:image\/jpeg;base64,/, `${file} hardcodes the JPEG prefix`);
|
|
assert.match(
|
|
source,
|
|
/\bJPEG_FRAME_DATA_URI_PREFIX\b/,
|
|
`${file} does not use the shared prefix`
|
|
);
|
|
}
|
|
});
|