|
5 | 5 | buildTranscriptDisplayBlocks, |
6 | 6 | flattenDisplayBlocks, |
7 | 7 | formatTurnSetupLabel, |
| 8 | + getDisplayBlockKey, |
8 | 9 | } from "./agentSessionTranscriptGrouping.ts"; |
9 | 10 | import { isObserverEventAfter } from "../observerRelayStore.ts"; |
10 | 11 |
|
@@ -1709,3 +1710,166 @@ test("buildTranscriptDisplayBlocks_leadingSingleBeforeSessionNew_emittedInline", |
1709 | 1710 | `sp (${spIdx}) must precede toolA (${toolAIdx}) — system-prompt must be hoisted even after a leading item`, |
1710 | 1711 | ); |
1711 | 1712 | }); |
| 1713 | + |
| 1714 | +// ── getDisplayBlockKey ────────────────────────────────────────────────────────── |
| 1715 | + |
| 1716 | +test("getDisplayBlockKey_single_returnsItemId", () => { |
| 1717 | + const block = { kind: "single", item: { id: "item-42" } }; |
| 1718 | + assert.equal(getDisplayBlockKey(block), "item-42"); |
| 1719 | +}); |
| 1720 | + |
| 1721 | +test("getDisplayBlockKey_turn_returnsPrefixedTurnId", () => { |
| 1722 | + const block = { kind: "turn", turnId: "t-99", segments: [] }; |
| 1723 | + assert.equal(getDisplayBlockKey(block), "turn:t-99"); |
| 1724 | +}); |
| 1725 | + |
| 1726 | +test("getDisplayBlockKey_sessionBoundary_usesFirstItemIdNotRunIndex", () => { |
| 1727 | + const block = { |
| 1728 | + kind: "session-boundary", |
| 1729 | + sessionId: "sess-2", |
| 1730 | + sessionStartTimestamp: "2026-07-08T00:00:00.000Z", |
| 1731 | + labelState: "most-recent", |
| 1732 | + runIndex: 3, |
| 1733 | + firstItemId: "first-item-abc", |
| 1734 | + }; |
| 1735 | + assert.equal( |
| 1736 | + getDisplayBlockKey(block), |
| 1737 | + "session-boundary:sess-2:first-item-abc", |
| 1738 | + "key must use firstItemId, not runIndex", |
| 1739 | + ); |
| 1740 | +}); |
| 1741 | + |
| 1742 | +test("getDisplayBlockKey_independentOfLatestLiveSessionId", () => { |
| 1743 | + // Build blocks for the same items with different latestLiveSessionId values. |
| 1744 | + // Keys must be identical — only labelState differs. |
| 1745 | + const items = [ |
| 1746 | + sessionItem("a", "sess-1", "2026-07-08T00:00:01.000Z"), |
| 1747 | + sessionItem("b", "sess-2", "2026-07-08T00:00:02.000Z"), |
| 1748 | + ]; |
| 1749 | + |
| 1750 | + const blocksLive = buildTranscriptDisplayBlocks(items, "sess-2"); |
| 1751 | + const blocksNoLive = buildTranscriptDisplayBlocks(items, null); |
| 1752 | + const blocksDiffLive = buildTranscriptDisplayBlocks(items, "sess-other"); |
| 1753 | + |
| 1754 | + const keysLive = blocksLive.map(getDisplayBlockKey); |
| 1755 | + const keysNoLive = blocksNoLive.map(getDisplayBlockKey); |
| 1756 | + const keysDiffLive = blocksDiffLive.map(getDisplayBlockKey); |
| 1757 | + |
| 1758 | + assert.deepEqual( |
| 1759 | + keysLive, |
| 1760 | + keysNoLive, |
| 1761 | + "keys must not vary with latestLiveSessionId=null", |
| 1762 | + ); |
| 1763 | + assert.deepEqual( |
| 1764 | + keysLive, |
| 1765 | + keysDiffLive, |
| 1766 | + "keys must not vary with latestLiveSessionId=other", |
| 1767 | + ); |
| 1768 | + |
| 1769 | + // Sanity check: labelState DID change (proves the test setup matters). |
| 1770 | + const boundaryLive = blocksLive.find((b) => b.kind === "session-boundary"); |
| 1771 | + const boundaryNoLive = blocksNoLive.find( |
| 1772 | + (b) => b.kind === "session-boundary", |
| 1773 | + ); |
| 1774 | + assert.notEqual( |
| 1775 | + boundaryLive.labelState, |
| 1776 | + boundaryNoLive.labelState, |
| 1777 | + "labelState must differ (test setup sanity check)", |
| 1778 | + ); |
| 1779 | +}); |
| 1780 | + |
| 1781 | +test("getDisplayBlockKey_parity_matchesBuildTranscriptDisplayBlocksOutput", () => { |
| 1782 | + // End-to-end: derive keys from buildTranscriptDisplayBlocks and assert they |
| 1783 | + // match the expected key for each block kind (the load-bearing invariant that |
| 1784 | + // outer-panel-derived ids match inner DOM data-message-id). |
| 1785 | + const items = [ |
| 1786 | + sessionItem("orphan", "sess-1", "2026-07-08T00:00:00.000Z"), |
| 1787 | + // Multi-session to get a boundary block. |
| 1788 | + sessionItem("tool-a", "sess-2", "2026-07-08T00:00:01.000Z"), |
| 1789 | + ]; |
| 1790 | + |
| 1791 | + // Give orphan no turnId so it becomes a "single" block. |
| 1792 | + items[0].turnId = undefined; |
| 1793 | + |
| 1794 | + const blocks = buildTranscriptDisplayBlocks(items, null); |
| 1795 | + const keys = blocks.map(getDisplayBlockKey); |
| 1796 | + |
| 1797 | + // Expect: [single:orphan, boundary:sess-2:tool-a, turn:turn-tool-a] |
| 1798 | + assert.ok(keys.includes("orphan"), "single block key = item.id"); |
| 1799 | + assert.ok( |
| 1800 | + keys.some((k) => k.startsWith("session-boundary:sess-2:")), |
| 1801 | + "boundary block key uses session-boundary: prefix", |
| 1802 | + ); |
| 1803 | + assert.ok( |
| 1804 | + keys.some((k) => k.startsWith("turn:")), |
| 1805 | + "turn block key uses turn: prefix", |
| 1806 | + ); |
| 1807 | + |
| 1808 | + // No duplicates. |
| 1809 | + assert.equal(new Set(keys).size, keys.length, "all keys must be unique"); |
| 1810 | +}); |
| 1811 | + |
| 1812 | +// ── Key-parity invariant: transient [turn, single] → [single, turn] reorder ── |
| 1813 | + |
| 1814 | +test("getDisplayBlockKey_firstTurnReorder_keysStableAcrossReorder", () => { |
| 1815 | + // Partial sequence: turn_started → session/new (two items, before session_resolved). |
| 1816 | + // At this point the key set is some combination of single + turn keys. |
| 1817 | + const ts = "2026-07-08T10:00:00.000Z"; |
| 1818 | + const partialItems = [ |
| 1819 | + { |
| 1820 | + id: "turn-started", |
| 1821 | + type: "lifecycle", |
| 1822 | + renderClass: "lifecycle", |
| 1823 | + title: "Turn started", |
| 1824 | + text: "", |
| 1825 | + timestamp: ts, |
| 1826 | + acpSource: "turn_started", |
| 1827 | + turnId: "turn-001", |
| 1828 | + sessionId: null, |
| 1829 | + channelId: "chan-1", |
| 1830 | + }, |
| 1831 | + { |
| 1832 | + id: "system-prompt:chan-1", |
| 1833 | + type: "metadata", |
| 1834 | + renderClass: "raw-rail", |
| 1835 | + title: "System prompt", |
| 1836 | + sections: [{ title: "Base", body: "You are a helpful AI assistant." }], |
| 1837 | + timestamp: ts, |
| 1838 | + acpSource: "session/new", |
| 1839 | + turnId: null, |
| 1840 | + sessionId: null, |
| 1841 | + channelId: "chan-1", |
| 1842 | + }, |
| 1843 | + ]; |
| 1844 | + |
| 1845 | + const partialBlocks = buildTranscriptDisplayBlocks(partialItems); |
| 1846 | + const partialKeys = new Set(partialBlocks.map(getDisplayBlockKey)); |
| 1847 | + |
| 1848 | + // Full sequence: add session_resolved (same turnId) — this seals the open |
| 1849 | + // batch and may reorder [turn, single] → [single, turn]. |
| 1850 | + const fullItems = [ |
| 1851 | + ...partialItems, |
| 1852 | + { |
| 1853 | + id: "session-resolved", |
| 1854 | + type: "lifecycle", |
| 1855 | + renderClass: "lifecycle", |
| 1856 | + title: "Session ready", |
| 1857 | + text: "", |
| 1858 | + timestamp: ts, |
| 1859 | + acpSource: "session_resolved", |
| 1860 | + turnId: "turn-001", |
| 1861 | + sessionId: "session-001", |
| 1862 | + channelId: "chan-1", |
| 1863 | + }, |
| 1864 | + ]; |
| 1865 | + |
| 1866 | + const fullBlocks = buildTranscriptDisplayBlocks(fullItems); |
| 1867 | + const fullKeys = new Set(fullBlocks.map(getDisplayBlockKey)); |
| 1868 | + |
| 1869 | + // The KEY IDENTITIES must be identical — only the ORDER may differ. |
| 1870 | + assert.deepEqual( |
| 1871 | + partialKeys, |
| 1872 | + fullKeys, |
| 1873 | + "block key identities must be identical before and after session_resolved seals the open batch (order may differ)", |
| 1874 | + ); |
| 1875 | +}); |
0 commit comments