Files
reverse-engineering-anything/src/domain/analysisView/byteBoundedViewRows.test.ts
T
ShuaixinHuangandVincent Koc 181fd31f16 fix(analysis-view): fit Android JNI row pages to the byte budget (#15276)
* fix(analysis-view): fit Android JNI row pages to the byte budget

* fix(views): preserve indexed selection in byte-bounded pages

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-12-01 01:48:30 +08:00

178 lines
4.8 KiB
TypeScript

import { expect, it } from "vitest";
import { ANALYSIS_VIEW_PAGE_BYTES } from "./analysisViewErrors.js";
import { selectByteBoundedViewRows } from "./byteBoundedViewRows.js";
const owner = {
parentEvidenceId: `ev_${"a".repeat(64)}`,
envelope: { kind: "probe" },
facet: "rows",
};
it("projects only the page's rows when unfiltered", () => {
const projected: number[] = [];
const page = selectByteBoundedViewRows(
Array.from({ length: 1_000 }, (_, index) => index),
{ offset: 10, limit: 3 },
{
matches: undefined,
project: (row) => {
projected.push(row);
return { row };
},
},
owner,
);
expect(page).toEqual({
ok: true,
value: {
rows: [{ row: 10 }, { row: 11 }, { row: 12 }],
total: 1_000,
byte_limited: false,
},
});
expect(projected).toEqual([10, 11, 12]);
});
it("counts every match after a byte-limited page", () => {
const big = "x".repeat(ANALYSIS_VIEW_PAGE_BYTES / 3);
const page = selectByteBoundedViewRows(
Array.from({ length: 8 }, (_, index) => ({ index, big })),
{ offset: 0, limit: 64 },
{ matches: (row) => row.index % 2 === 0, project: (row) => row },
owner,
);
if (!page.ok) throw page.error;
expect(page.value.rows.map((row) => row.index)).toEqual([0, 2]);
expect(page.value).toMatchObject({ total: 4, byte_limited: true });
});
it("refuses a row that cannot fit a page alone and names the parent", () => {
const page = selectByteBoundedViewRows(
["x".repeat(ANALYSIS_VIEW_PAGE_BYTES)],
{ offset: 0, limit: 1 },
{ matches: undefined, project: (row) => row },
owner,
);
expect(page).toMatchObject({
ok: false,
error: {
_tag: "AnalysisResourceConstraintError",
reportedLimits: {
parent_evidence_id: owner.parentEvidenceId,
facet: "rows",
limit_bytes: ANALYSIS_VIEW_PAGE_BYTES,
},
},
});
});
it("visits only valid indexed candidates before filtering and offset", () => {
const visited: number[] = [];
const projected: number[] = [];
const page = selectByteBoundedViewRows(
Array.from({ length: 10_000 }, (_, index) => index),
{ offset: 1, limit: 1 },
{
candidateIndices: new Set([9_999, 41, 2, -1, 10_000, 1.5, NaN]),
matches: (row) => {
visited.push(row);
return row !== 41;
},
project: (row, index) => {
projected.push(row);
return { row, index };
},
},
owner,
);
expect(page).toEqual({
ok: true,
value: {
rows: [{ row: 9_999, index: 9_999 }],
total: 2,
byte_limited: false,
},
});
expect(visited).toEqual([2, 41, 9_999]);
expect(projected).toEqual([9_999]);
});
it("keeps an empty indexed selection empty without visiting rows", () => {
const page = selectByteBoundedViewRows(
[1, 2, 3],
{ offset: 0, limit: 3 },
{
candidateIndices: new Set(),
matches: () => {
throw new Error("empty selection visited a row");
},
project: (row) => row,
},
owner,
);
expect(page).toEqual({
ok: true,
value: { rows: [], total: 0, byte_limited: false },
});
});
it("continues byte-limited indexed pages in original row order", () => {
const big = "x".repeat(ANALYSIS_VIEW_PAGE_BYTES / 3);
const rows = Array.from({ length: 16 }, (_, index) => ({
index,
big,
}));
const visited: number[] = [];
const projected: number[] = [];
const page = (offset: number) =>
selectByteBoundedViewRows(
rows,
{ offset, limit: 4 },
{
candidateIndices: new Set([15, 11, 7, 1]),
matches: (row) => {
visited.push(row.index);
return true;
},
project: (row) => {
projected.push(row.index);
return row;
},
},
owner,
);
const first = page(0);
if (!first.ok) throw first.error;
expect(first.value.rows.map((row) => row.index)).toEqual([1, 7]);
expect(first.value).toMatchObject({ total: 4, byte_limited: true });
expect(visited).toEqual([1, 7, 11, 15]);
expect(projected).toEqual([1, 7, 11]);
const second = page(first.value.rows.length);
if (!second.ok) throw second.error;
expect(second.value.rows.map((row) => row.index)).toEqual([11, 15]);
expect(second.value).toMatchObject({ total: 4, byte_limited: false });
});
it("pages unfiltered indexed rows without projecting skipped candidates", () => {
const projected: number[] = [];
const page = selectByteBoundedViewRows(
Array.from({ length: 8 }, (_, index) => index),
{ offset: 1, limit: 2 },
{
candidateIndices: new Set([7, 1, 4]),
matches: undefined,
project: (row, index) => {
projected.push(index);
return row;
},
},
owner,
);
expect(page).toEqual({
ok: true,
value: { rows: [4, 7], total: 3, byte_limited: false },
});
expect(projected).toEqual([4, 7]);
});