Open History to see the last commit
1import { beforeEach, describe, expect, test } from 'vitest'
2import { Index } from '../src/indexes.js'
3
4describe(`Index`, () => {
5 test(`an index built without presence throws when presence is read`, () => {
6 const index = new Index<string, number>({ trackPresence: false })
7 index.addValue(`k`, [1, 1])
8 expect(index.get(`k`)).toEqual([[1, 1]])
9 expect(() => index.hasPresence(`k`)).toThrow(`does not track presence`)
10 expect(() => index.getConsolidatedMultiplicity(`k`)).toThrow(
11 `does not track presence`,
12 )
13 expect(() => [...index.getPresenceKeys()]).toThrow(
14 `does not track presence`,
15 )
16 })
17
18 let index: Index<string, number>
19 let createIndex: (name: string) => Index<string, number>
20
21 beforeEach(() => {
22 createIndex = (_name: string) => new Index()
23 index = new Index()
24 })
25
26 describe(`basic operations`, () => {
27 test(`should add and get values`, () => {
28 index.addValue(`key1`, [10, 1])
29 index.addValue(`key1`, [20, 2])
30
31 const result = index.get(`key1`)
32 expect(result).toEqual([
33 [10, 1],
34 [20, 2],
35 ])
36 })
37
38 test(`should return empty array for non-existent key`, () => {
39 const result = index.get(`nonexistent`)
40 expect(result).toEqual([])
41 })
42
43 test(`should handle zero multiplicities`, () => {
44 index.addValue(`key1`, [10, 1])
45 index.addValue(`key1`, [10, -1])
46
47 // Value is compacted on the fly
48 const result = index.get(`key1`)
49 expect(result).toEqual([])
50 })
51 })
52
53 describe(`append`, () => {
54 test(`should append data from another index`, () => {
55 const other = createIndex(`other`)
56
57 index.addValue(`key1`, [10, 1])
58 other.addValue(`key1`, [20, 1])
59 other.addValue(`key2`, [30, 1])
60
61 index.append(other)
62
63 expect(index.get(`key1`)).toEqual([
64 [10, 1],
65 [20, 1],
66 ])
67 expect(index.get(`key2`)).toEqual([[30, 1]])
68 })
69
70 test(`should combine multiplicities for same values`, () => {
71 const other = createIndex(`other`)
72
73 index.addValue(`key1`, [10, 2])
74 other.addValue(`key1`, [10, 3])
75
76 index.append(other)
77
78 expect(index.get(`key1`)).toEqual([[10, 5]])
79 })
80 })
81
82 describe(`join`, () => {
83 test(`should join two indexes`, () => {
84 const other = createIndex(`other`)
85
86 index.addValue(`key1`, [10, 2])
87 other.addValue(`key1`, [20, 3])
88
89 const result = index.join(other)
90 const entries = result.getInner()
91 expect(entries).toHaveLength(1)
92 expect(entries[0]).toEqual([[`key1`, [10, 20]], 6])
93 })
94
95 test(`should return empty multiset when no matching keys`, () => {
96 const other = createIndex(`other`)
97
98 index.addValue(`key1`, [10, 1])
99 other.addValue(`key2`, [20, 1])
100
101 const result = index.join(other)
102 expect(result.getInner()).toEqual([])
103 })
104
105 test(`should handle multiple values with same key`, () => {
106 const other = createIndex(`other`)
107
108 index.addValue(`key1`, [10, 2])
109 index.addValue(`key1`, [20, 3])
110 other.addValue(`key1`, [30, 1])
111 other.addValue(`key1`, [40, 2])
112
113 const result = index.join(other)
114 const entries = result.getInner()
115 expect(entries).toHaveLength(4)
116 expect(entries).toContainEqual([[`key1`, [10, 30]], 2])
117 expect(entries).toContainEqual([[`key1`, [10, 40]], 4])
118 expect(entries).toContainEqual([[`key1`, [20, 30]], 3])
119 expect(entries).toContainEqual([[`key1`, [20, 40]], 6])
120 })
121
122 test(`should handle multiple keys`, () => {
123 const other = createIndex(`other`)
124
125 index.addValue(`key1`, [10, 2])
126 index.addValue(`key2`, [20, 3])
127 other.addValue(`key1`, [30, 1])
128 other.addValue(`key2`, [40, 2])
129
130 const result = index.join(other)
131 const entries = result.getInner()
132 expect(entries).toHaveLength(2)
133 expect(entries).toContainEqual([[`key1`, [10, 30]], 2])
134 expect(entries).toContainEqual([[`key2`, [20, 40]], 6])
135 })
136
137 test(`should handle zero multiplicities correctly`, () => {
138 const other = createIndex(`other`)
139
140 index.addValue(`key1`, [10, 0])
141 other.addValue(`key1`, [20, 2])
142
143 const result = index.join(other)
144 expect(result.getInner()).toEqual([])
145 })
146 })
147
148 test(`should track and compact only changed keys`, () => {
149 // Add values to different keys
150 index.addValue(`key1`, [10, 1])
151 index.addValue(`key1`, [10, -1]) // This should cancel out when compacted
152 index.addValue(`key2`, [20, 1])
153 index.addValue(`key3`, [30, 1])
154
155 // Create another index that we'll append from
156 const other = createIndex(`other`)
157 other.addValue(`key4`, [40, 1])
158 other.addValue(`key4`, [40, -1]) // This should cancel out when compacted
159 other.addValue(`key5`, [50, 1])
160
161 // Append should also track changed keys
162 index.append(other)
163
164 // Values should be consolidated and zero-multiplicity entries removed
165 expect(index.get(`key1`)).toEqual([]) // Cancelled out
166 expect(index.get(`key2`)).toEqual([[20, 1]])
167 expect(index.get(`key3`)).toEqual([[30, 1]])
168 expect(index.get(`key4`)).toEqual([]) // Cancelled out
169 expect(index.get(`key5`)).toEqual([[50, 1]])
170
171 // Add more values
172 index.addValue(`key2`, [25, 1])
173 index.addValue(`key6`, [60, 1])
174
175 expect(index.get(`key2`)).toEqual([
176 [20, 1],
177 [25, 1],
178 ]) // Both values preserved
179 expect(index.get(`key6`)).toEqual([[60, 1]])
180
181 // Other keys should remain unchanged
182 expect(index.get(`key3`)).toEqual([[30, 1]])
183 expect(index.get(`key5`)).toEqual([[50, 1]])
184 })
185})