Skip to content

Commit f37989e

Browse files
committed
Add tests for lww update
1 parent 25a7f18 commit f37989e

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

packages/core/src/database/database.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,69 @@ describe('DeclarativeDatabase', () => {
125125

126126
await expect(uninitDb.insert('users', { name: 'Alice' })).rejects.toThrow();
127127
});
128+
129+
it('should update LWW column HLC timestamps on update', async () => {
130+
// Create a new database with LWW columns
131+
const schemaWithLww = new SchemaBuilder()
132+
.table('items', t => {
133+
t.guid('id').notNull('');
134+
t.text('name').notNull('');
135+
t.text('rowstate').notNull('').lww();
136+
t.key('id').primary();
137+
})
138+
.build();
139+
140+
const dbWithLww = new DeclarativeDatabase({
141+
adapter,
142+
schema: schemaWithLww,
143+
autoMigrate: true,
144+
});
145+
146+
await dbWithLww.initialize();
147+
148+
// Insert a record
149+
const systemId = await dbWithLww.insert('items', {
150+
id: '123',
151+
name: 'Test Item',
152+
rowstate: 'active',
153+
});
154+
155+
// Get the initial HLC timestamp for rowstate
156+
const initialRecord = await dbWithLww.queryOne('items', {
157+
where: 'system_id = ?',
158+
whereArgs: [systemId],
159+
});
160+
161+
const initialHlc = initialRecord?.rowstate__hlc;
162+
expect(initialHlc).toBeDefined();
163+
164+
// Wait a moment to ensure time advances
165+
await new Promise(resolve => setTimeout(resolve, 10));
166+
167+
// Update the rowstate
168+
await dbWithLww.update(
169+
'items',
170+
{ rowstate: 'inactive' },
171+
{ where: 'system_id = ?', whereArgs: [systemId] }
172+
);
173+
174+
// Get the updated record
175+
const updatedRecord = await dbWithLww.queryOne('items', {
176+
where: 'system_id = ?',
177+
whereArgs: [systemId],
178+
});
179+
180+
// Verify the rowstate was updated
181+
expect(updatedRecord?.rowstate).toBe('inactive');
182+
183+
// Verify the HLC timestamp was updated (should be different/newer)
184+
const updatedHlc = updatedRecord?.rowstate__hlc;
185+
expect(updatedHlc).toBeDefined();
186+
expect(updatedHlc).not.toBe(initialHlc);
187+
188+
// The updated HLC should be lexicographically greater (newer)
189+
expect(updatedHlc! > initialHlc!).toBe(true);
190+
191+
await dbWithLww.close();
192+
});
128193
});

0 commit comments

Comments
 (0)