Skip to content

Commit 0ee40ee

Browse files
committed
Add tests
1 parent ba03fe0 commit 0ee40ee

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

packages/collaboration/src/__tests__/CollaborationActor.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,62 @@ describe('CollaborationActor', () => {
424424
expect(client.state.users['user-2'].profile.tags).toContain('tag2');
425425
});
426426
});
427+
428+
describe('Offline-First Support', () => {
429+
it('should allow setState before any peers connect', async () => {
430+
const client = system.getClient(TodosToken)!;
431+
432+
// No connection - completely offline
433+
client.actions.addTodo('Offline todo');
434+
await flushMicrotask();
435+
436+
expect(client.state.todos.length).toBe(1);
437+
expect(client.state.todos[0].text).toBe('Offline todo');
438+
});
439+
440+
it('should accumulate multiple setState calls before connection', async () => {
441+
const client = system.getClient(TodosToken)!;
442+
443+
// Multiple changes while offline
444+
client.actions.addTodo('Todo 1');
445+
await new Promise(resolve => setTimeout(resolve, 1)); // Ensure unique timestamp
446+
client.actions.addTodo('Todo 2');
447+
await new Promise(resolve => setTimeout(resolve, 1)); // Ensure unique timestamp
448+
client.actions.addTodo('Todo 3');
449+
await flushMicrotask();
450+
451+
expect(client.state.todos.length).toBe(3);
452+
453+
// Toggle one todo
454+
const todoId = client.state.todos[1].id;
455+
client.actions.toggleTodo(todoId);
456+
await flushMicrotask();
457+
458+
expect(client.state.todos[1].done).toBe(true);
459+
});
460+
461+
it('should maintain state consistency across offline operations', async () => {
462+
const client = system.getClient(TodosToken)!;
463+
464+
// Complex offline workflow
465+
client.actions.addTodo('Task 1');
466+
await flushMicrotask();
467+
const id1 = client.state.todos[0].id;
468+
469+
client.actions.addTodo('Task 2');
470+
await flushMicrotask();
471+
472+
client.actions.toggleTodo(id1);
473+
await flushMicrotask();
474+
475+
client.actions.addTodo('Task 3');
476+
await flushMicrotask();
477+
478+
// Verify final state
479+
expect(client.state.todos.length).toBe(3);
480+
expect(client.state.todos[0].done).toBe(true);
481+
expect(client.state.todos[1].done).toBe(false);
482+
expect(client.state.todos[2].done).toBe(false);
483+
});
484+
});
427485
});

packages/collaboration/src/__tests__/PeerMessagingActor.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,5 @@ describe('PeerMessagingActor', () => {
172172
expect(() => client.actions.broadcast(message)).not.toThrow();
173173
});
174174
});
175+
175176
});

packages/collaboration/src/__tests__/integration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,5 @@ describe('Integration: Error Handling and Edge Cases', () => {
474474
expect(counter.state.count).toBe(100);
475475
expect(counter.state.lastEditor).toBe('op5');
476476
});
477+
477478
});

0 commit comments

Comments
 (0)