Skip to content

Commit b6a1ad7

Browse files
committed
fix(web): bound cached pull request details
1 parent 84aebb7 commit b6a1ad7

2 files changed

Lines changed: 378 additions & 20 deletions

File tree

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts‎

Lines changed: 259 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ProjectId,
23
PullRequestAction,
34
type PullRequestCheck,
45
type PullRequestComment,
@@ -31,6 +32,8 @@ import {
3132
pullRequestFindingKey,
3233
pullRequestHandoffLabels,
3334
pullRequestReviewOutcome,
35+
PULL_REQUEST_DETAIL_SNAPSHOT_MAX_BYTES,
36+
PULL_REQUEST_DETAIL_SNAPSHOT_MAX_ENTRIES,
3437
readableFailure,
3538
readPullRequestDetailSnapshot,
3639
resolveDisplayedPullRequestDetail,
@@ -1313,7 +1316,9 @@ describe("which actions need the host read again after they run", () => {
13131316
});
13141317

13151318
describe("cached pull request detail", () => {
1316-
const reference = { projectId: "project-1", repository: "acme/web", number: 7 };
1319+
const reference = { projectId: ProjectId.make("project-1"), repository: "acme/web", number: 7 };
1320+
const snapshotPrefix = "t3.pullRequests.detail.v1:";
1321+
const indexKey = "t3.pullRequests.detail.index.v1";
13171322
const detail = (overrides: Partial<PullRequestDetail> = {}): PullRequestDetail =>
13181323
({
13191324
provider: "github",
@@ -1371,6 +1376,12 @@ describe("cached pull request detail", () => {
13711376
return {
13721377
getItem: (key: string) => held.get(key) ?? null,
13731378
setItem: (key: string, value: string) => void held.set(key, value),
1379+
removeItem: (key: string) => void held.delete(key),
1380+
key: (position: number) => [...held.keys()][position] ?? null,
1381+
get length() {
1382+
return held.size;
1383+
},
1384+
entries: () => [...held.entries()],
13741385
};
13751386
};
13761387

@@ -1384,6 +1395,233 @@ describe("cached pull request detail", () => {
13841395
expect(snapshot?.deletions).toBe(3);
13851396
});
13861397

1398+
it.each([
1399+
{ environmentId: "env-2", reference },
1400+
{ environmentId: "env-1", reference: { ...reference, projectId: ProjectId.make("project-2") } },
1401+
{ environmentId: "env-1", reference: { ...reference, repository: "acme/api" } },
1402+
{ environmentId: "env-1", reference: { ...reference, number: 8 } },
1403+
])("isolates the cache by environment, project, repository, and number: %j", (other) => {
1404+
const storage = makeStorage();
1405+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1406+
expect(readPullRequestDetailSnapshot(storage, other.environmentId, other.reference)).toBeNull();
1407+
writePullRequestDetailSnapshot(
1408+
storage,
1409+
other.environmentId,
1410+
other.reference,
1411+
detail({ ...other.reference, title: "Another PR" }),
1412+
);
1413+
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)?.title).toBe(
1414+
"Cache the title",
1415+
);
1416+
expect(
1417+
readPullRequestDetailSnapshot(storage, other.environmentId, other.reference)?.title,
1418+
).toBe("Another PR");
1419+
});
1420+
1421+
it("keeps scope components distinct when they contain the old key separators", () => {
1422+
const storage = makeStorage();
1423+
const first = { ...reference, projectId: ProjectId.make("project:one") };
1424+
const second = { ...reference, projectId: ProjectId.make("one") };
1425+
writePullRequestDetailSnapshot(storage, "env", first, detail({ ...first, title: "First" }));
1426+
writePullRequestDetailSnapshot(
1427+
storage,
1428+
"env:project",
1429+
second,
1430+
detail({ ...second, title: "Second" }),
1431+
);
1432+
expect(readPullRequestDetailSnapshot(storage, "env", first)?.title).toBe("First");
1433+
expect(readPullRequestDetailSnapshot(storage, "env:project", second)?.title).toBe("Second");
1434+
});
1435+
1436+
it("evicts the least recently read or written detail across environments", () => {
1437+
const storage = makeStorage();
1438+
for (let number = 1; number <= PULL_REQUEST_DETAIL_SNAPSHOT_MAX_ENTRIES; number++) {
1439+
writePullRequestDetailSnapshot(
1440+
storage,
1441+
`env-${number}`,
1442+
{ ...reference, number },
1443+
detail({ number }),
1444+
);
1445+
}
1446+
expect(
1447+
readPullRequestDetailSnapshot(storage, "env-1", { ...reference, number: 1 }),
1448+
).not.toBeNull();
1449+
writePullRequestDetailSnapshot(
1450+
storage,
1451+
"env-2",
1452+
{ ...reference, number: 2 },
1453+
detail({ number: 2 }),
1454+
);
1455+
writePullRequestDetailSnapshot(storage, "env-new", reference, detail());
1456+
expect(readPullRequestDetailSnapshot(storage, "env-3", { ...reference, number: 3 })).toBeNull();
1457+
expect(
1458+
readPullRequestDetailSnapshot(storage, "env-1", { ...reference, number: 1 }),
1459+
).not.toBeNull();
1460+
expect(
1461+
readPullRequestDetailSnapshot(storage, "env-2", { ...reference, number: 2 }),
1462+
).not.toBeNull();
1463+
expect(storage.entries().filter(([key]) => key.startsWith(snapshotPrefix))).toHaveLength(
1464+
PULL_REQUEST_DETAIL_SNAPSHOT_MAX_ENTRIES,
1465+
);
1466+
});
1467+
1468+
it("bounds UTF-16 storage bytes, including keys and the recency index", () => {
1469+
const storage = makeStorage();
1470+
const body = "🚀".repeat(PULL_REQUEST_DETAIL_SNAPSHOT_MAX_BYTES / 16);
1471+
for (let number = 1; number <= 4; number++) {
1472+
writePullRequestDetailSnapshot(
1473+
storage,
1474+
"env-1",
1475+
{ ...reference, number },
1476+
detail({ number, body }),
1477+
);
1478+
}
1479+
expect(readPullRequestDetailSnapshot(storage, "env-1", { ...reference, number: 1 })).toBeNull();
1480+
expect(readPullRequestDetailSnapshot(storage, "env-1", { ...reference, number: 4 })?.body).toBe(
1481+
body,
1482+
);
1483+
const bytes = storage
1484+
.entries()
1485+
.reduce((total, [key, value]) => total + 2 * (key.length + value.length), 0);
1486+
expect(bytes).toBeLessThanOrEqual(PULL_REQUEST_DETAIL_SNAPSHOT_MAX_BYTES);
1487+
1488+
const longEnvironmentId = "e".repeat(PULL_REQUEST_DETAIL_SNAPSHOT_MAX_BYTES / 4);
1489+
writePullRequestDetailSnapshot(storage, longEnvironmentId, reference, detail());
1490+
expect(readPullRequestDetailSnapshot(storage, longEnvironmentId, reference)).toBeNull();
1491+
expect(readPullRequestDetailSnapshot(storage, "env-1", { ...reference, number: 4 })?.body).toBe(
1492+
body,
1493+
);
1494+
});
1495+
1496+
it("drops an oversized replacement without evicting other details", () => {
1497+
const storage = makeStorage();
1498+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1499+
writePullRequestDetailSnapshot(storage, "env-2", reference, detail());
1500+
writePullRequestDetailSnapshot(
1501+
storage,
1502+
"env-1",
1503+
reference,
1504+
detail({ body: "x".repeat(PULL_REQUEST_DETAIL_SNAPSHOT_MAX_BYTES) }),
1505+
);
1506+
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)).toBeNull();
1507+
expect(readPullRequestDetailSnapshot(storage, "env-2", reference)?.title).toBe(
1508+
"Cache the title",
1509+
);
1510+
expect(storage.entries().filter(([key]) => key.startsWith(snapshotPrefix))).toHaveLength(1);
1511+
});
1512+
1513+
it("repairs stale byte accounting even when the next detail is oversized", () => {
1514+
const storage = makeStorage();
1515+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1516+
const key = storage.entries().find(([key]) => key.startsWith(snapshotPrefix))![0];
1517+
const oversized = detail({ body: "x".repeat(PULL_REQUEST_DETAIL_SNAPSHOT_MAX_BYTES) });
1518+
storage.setItem(key, JSON.stringify(oversized));
1519+
writePullRequestDetailSnapshot(storage, "env-2", reference, oversized);
1520+
const bytes = storage
1521+
.entries()
1522+
.reduce((total, [storedKey, value]) => total + 2 * (storedKey.length + value.length), 0);
1523+
expect(bytes).toBeLessThanOrEqual(PULL_REQUEST_DETAIL_SNAPSHOT_MAX_BYTES);
1524+
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)).toBeNull();
1525+
expect(readPullRequestDetailSnapshot(storage, "env-2", reference)).toBeNull();
1526+
});
1527+
1528+
it("retires legacy and unindexed detail keys without touching other storage", () => {
1529+
const storage = makeStorage();
1530+
const retired = [
1531+
"t3.pullRequests.detail:env-1:project-1:acme/web#1",
1532+
"t3.pullRequests.detail:env-2:project-2:acme/api#2",
1533+
`${snapshotPrefix}interrupted-write`,
1534+
];
1535+
const unrelated = ["t3.pullRequests.list:env-1", "t3.pullRequests.detailOther", "settings"];
1536+
for (const key of [...retired, ...unrelated]) storage.setItem(key, "keep if unrelated");
1537+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1538+
for (const key of retired) expect(storage.getItem(key)).toBeNull();
1539+
for (const key of unrelated) expect(storage.getItem(key)).toBe("keep if unrelated");
1540+
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)?.title).toBe(
1541+
"Cache the title",
1542+
);
1543+
});
1544+
1545+
it.each(["{not json", JSON.stringify([{ key: "settings", bytes: 20 }])])(
1546+
"recovers from a corrupt index without deleting unrelated data: %s",
1547+
(index) => {
1548+
const storage = makeStorage();
1549+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1550+
storage.setItem("settings", "keep");
1551+
storage.setItem(indexKey, index);
1552+
writePullRequestDetailSnapshot(storage, "env-2", reference, detail());
1553+
expect(storage.getItem("settings")).toBe("keep");
1554+
expect(readPullRequestDetailSnapshot(storage, "env-2", reference)?.title).toBe(
1555+
"Cache the title",
1556+
);
1557+
expect(storage.entries().filter(([key]) => key.startsWith(snapshotPrefix))).toHaveLength(1);
1558+
},
1559+
);
1560+
1561+
it("reads only the selected payload and keeps it when a recency update fails", () => {
1562+
const storage = makeStorage();
1563+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1564+
const selectedKey = storage.entries().find(([key]) => key.startsWith(snapshotPrefix))![0];
1565+
writePullRequestDetailSnapshot(storage, "env-2", reference, detail());
1566+
const getItem = storage.getItem;
1567+
storage.getItem = (key) => {
1568+
if (key !== selectedKey && key !== indexKey) throw new Error("Read another payload");
1569+
return getItem(key);
1570+
};
1571+
storage.setItem = () => {
1572+
throw new Error("Storage is read-only");
1573+
};
1574+
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)?.title).toBe(
1575+
"Cache the title",
1576+
);
1577+
});
1578+
1579+
it.each(["payload", "index"] as const)(
1580+
"tolerates a failed %s write without leaving an unindexed payload",
1581+
(failedWrite) => {
1582+
const storage = makeStorage();
1583+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1584+
const setItem = storage.setItem;
1585+
storage.setItem = (key, value) => {
1586+
if ((key === indexKey) === (failedWrite === "index")) {
1587+
throw new DOMException("Storage quota exceeded", "QuotaExceededError");
1588+
}
1589+
setItem(key, value);
1590+
};
1591+
expect(() =>
1592+
writePullRequestDetailSnapshot(storage, "env-2", reference, detail()),
1593+
).not.toThrow();
1594+
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)?.title).toBe(
1595+
"Cache the title",
1596+
);
1597+
expect(readPullRequestDetailSnapshot(storage, "env-2", reference)).toBeNull();
1598+
expect(storage.entries().filter(([key]) => key.startsWith(snapshotPrefix))).toHaveLength(1);
1599+
storage.setItem = setItem;
1600+
writePullRequestDetailSnapshot(storage, "env-2", reference, detail());
1601+
expect(readPullRequestDetailSnapshot(storage, "env-2", reference)?.title).toBe(
1602+
"Cache the title",
1603+
);
1604+
},
1605+
);
1606+
1607+
it("drops a replacement if the new payload cannot be recorded in the index", () => {
1608+
const storage = makeStorage();
1609+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1610+
const setItem = storage.setItem;
1611+
storage.setItem = (key, value) => {
1612+
if (key === indexKey) throw new Error("Index write failed");
1613+
setItem(key, value);
1614+
};
1615+
writePullRequestDetailSnapshot(
1616+
storage,
1617+
"env-1",
1618+
reference,
1619+
detail({ body: "larger".repeat(100) }),
1620+
);
1621+
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)).toBeNull();
1622+
expect(storage.entries().filter(([key]) => key.startsWith(snapshotPrefix))).toHaveLength(0);
1623+
});
1624+
13871625
it("keeps a cached tab painted while the live read replaces the counts", () => {
13881626
const cached = detail();
13891627
const live = detail({ additions: 40, deletions: 9, title: "Cache the title" });
@@ -1404,10 +1642,28 @@ describe("cached pull request detail", () => {
14041642
expect(readPullRequestDetailSnapshot(makeStorage(), "env-2", reference)).toBeNull();
14051643
});
14061644

1407-
it("shrugs off corrupt storage and no storage at all", () => {
1645+
it("tolerates corrupt, denied, missing, and non-evictable storage", () => {
14081646
const storage = makeStorage();
1409-
storage.setItem("t3.pullRequests.detail:env-1:project-1:acme/web#7", "{not json");
1647+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
1648+
const key = storage.entries().find(([key]) => key.startsWith(snapshotPrefix))![0];
1649+
storage.setItem(key, "{not json");
14101650
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)).toBeNull();
14111651
expect(readPullRequestDetailSnapshot(undefined, "env-1", reference)).toBeNull();
1652+
expect(() =>
1653+
writePullRequestDetailSnapshot(undefined, "env-1", reference, detail()),
1654+
).not.toThrow();
1655+
const limited = { getItem: storage.getItem, setItem: storage.setItem };
1656+
writePullRequestDetailSnapshot(limited, "env-2", reference, detail());
1657+
expect(readPullRequestDetailSnapshot(limited, "env-2", reference)).toBeNull();
1658+
storage.getItem = () => {
1659+
throw new Error("Storage is denied");
1660+
};
1661+
storage.setItem = () => {
1662+
throw new Error("Storage is denied");
1663+
};
1664+
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)).toBeNull();
1665+
expect(() =>
1666+
writePullRequestDetailSnapshot(storage, "env-1", reference, detail()),
1667+
).not.toThrow();
14121668
});
14131669
});

0 commit comments

Comments
 (0)