Open History to see the last commit
1import { expect } from 'vitest'
2import { getLoadedIds } from './helpers'
3import type { Collection } from '@tanstack/db'
4
5/**
6 * Assert that a collection has loaded exactly the expected items (no more, no less)
7 */
8export function assertLoadedExactly<T extends { id: string }>(
9 collection: Collection<T>,
10 expectedIds: Array<string>,
11 message?: string,
12) {
13 const loadedIds = getLoadedIds(collection)
14 const loadedSet = new Set(loadedIds)
15 const expectedSet = new Set(expectedIds)
16
17 // Check for extra items
18 const extraIds = loadedIds.filter((id) => !expectedSet.has(id))
19 if (extraIds.length > 0) {
20 throw new Error(
21 message ??
22 `Collection has extra items: ${extraIds.join(`, `)} (expected only: ${expectedIds.join(`, `)})`,
23 )
24 }
25
26 // Check for missing items
27 const missingIds = expectedIds.filter((id) => !loadedSet.has(id))
28 if (missingIds.length > 0) {
29 throw new Error(
30 message ??
31 `Collection is missing items: ${missingIds.join(`, `)} (loaded: ${loadedIds.join(`, `)})`,
32 )
33 }
34}
35
36/**
37 * Assert that a collection has loaded at least the expected items (may have more)
38 */
39export function assertLoadedAtLeast<T extends { id: string }>(
40 collection: Collection<T>,
41 expectedIds: Array<string>,
42 message?: string,
43) {
44 const loadedIds = getLoadedIds(collection)
45 const loadedSet = new Set(loadedIds)
46
47 const missingIds = expectedIds.filter((id) => !loadedSet.has(id))
48 if (missingIds.length > 0) {
49 throw new Error(
50 message ??
51 `Collection is missing items: ${missingIds.join(`, `)} (loaded: ${loadedIds.join(`, `)})`,
52 )
53 }
54}
55
56/**
57 * Assert that a collection has NOT loaded any of the specified items
58 */
59export function assertNotLoaded<T extends { id: string }>(
60 collection: Collection<T>,
61 forbiddenIds: Array<string>,
62 message?: string,
63) {
64 const loadedIds = getLoadedIds(collection)
65 const loadedSet = new Set(loadedIds)
66
67 const foundIds = forbiddenIds.filter((id) => loadedSet.has(id))
68 if (foundIds.length > 0) {
69 throw new Error(
70 message ?? `Collection should not have loaded: ${foundIds.join(`, `)}`,
71 )
72 }
73}
74
75/**
76 * Assert that a collection's size matches expected
77 */
78export function assertCollectionSize<T extends object>(
79 collection: Collection<T>,
80 expectedSize: number,
81 message?: string,
82) {
83 expect(collection.size, message).toBe(expectedSize)
84}
85
86/**
87 * Assert that all items in a collection match a predicate
88 */
89export function assertAllItemsMatch<T extends object>(
90 collection: Collection<T>,
91 predicate: (item: T) => boolean,
92 message?: string,
93) {
94 const items = Array.from(collection.state.values())
95 const failingItems = items.filter((item) => !predicate(item))
96
97 if (failingItems.length > 0) {
98 throw new Error(
99 message ??
100 `${failingItems.length} item(s) did not match predicate: ${JSON.stringify(failingItems[0])}`,
101 )
102 }
103}
104
105/**
106 * Assert that items are sorted correctly
107 */
108export function assertSorted<T, K extends keyof T>(
109 items: Array<T>,
110 field: K,
111 direction: `asc` | `desc` = `asc`,
112 message?: string,
113) {
114 for (let i = 1; i < items.length; i++) {
115 const prev = items[i - 1]![field]
116 const curr = items[i]![field]
117
118 if (direction === `asc`) {
119 if (prev > curr) {
120 throw new Error(
121 message ??
122 `Items not sorted ascending by ${String(field)}: ${prev} > ${curr} at index ${i}`,
123 )
124 }
125 } else {
126 if (prev < curr) {
127 throw new Error(
128 message ??
129 `Items not sorted descending by ${String(field)}: ${prev} < ${curr} at index ${i}`,
130 )
131 }
132 }
133 }
134}
135
136/**
137 * Assert that predicate pushdown occurred (no over-fetching)
138 * This checks that the collection didn't load more data than necessary
139 */
140export function assertNoPushdownViolation<T extends { id: string }>(
141 collection: Collection<T>,
142 expectedMaxIds: Array<string>,
143 message?: string,
144) {
145 const loadedIds = getLoadedIds(collection)
146 const expectedSet = new Set(expectedMaxIds)
147
148 // Check if any loaded IDs are not in expected set
149 const extraIds = loadedIds.filter((id) => !expectedSet.has(id))
150
151 if (extraIds.length > 0) {
152 throw new Error(
153 message ??
154 `Predicate pushdown violation: Collection loaded ${extraIds.length} extra item(s) that don't match the predicate`,
155 )
156 }
157}
158
159/**
160 * Assert that deduplication occurred
161 */
162export function assertDeduplicationOccurred(
163 actualLoads: number,
164 deduplicatedLoads: number,
165 expectedActualLoads: number,
166 message?: string,
167) {
168 expect(actualLoads, message ?? `Actual loads`).toBe(expectedActualLoads)
169 expect(deduplicatedLoads, message ?? `Deduplicated loads`).toBeGreaterThan(0)
170}
171
172/**
173 * Assert that NO deduplication occurred (all requests were unique)
174 */
175export function assertNoDeduplication(
176 actualLoads: number,
177 deduplicatedLoads: number,
178 message?: string,
179) {
180 expect(deduplicatedLoads, message ?? `Deduplicated loads`).toBe(0)
181 expect(actualLoads, message ?? `Actual loads`).toBeGreaterThan(0)
182}