@vuer-ai/vuer-rtc 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/EditBuffer.d.ts +43 -0
- package/dist/client/EditBuffer.d.ts.map +1 -0
- package/dist/client/EditBuffer.js +96 -0
- package/dist/client/EditBuffer.js.map +1 -0
- package/dist/client/actions.d.ts +66 -0
- package/dist/client/actions.d.ts.map +1 -0
- package/dist/client/actions.js +345 -0
- package/dist/client/actions.js.map +1 -0
- package/dist/client/createGraph.d.ts +30 -0
- package/dist/client/createGraph.d.ts.map +1 -0
- package/dist/client/createGraph.js +91 -0
- package/dist/client/createGraph.js.map +1 -0
- package/dist/client/hooks.d.ts +81 -0
- package/dist/client/hooks.d.ts.map +1 -0
- package/dist/client/hooks.js +161 -0
- package/dist/client/hooks.js.map +1 -0
- package/dist/client/index.d.ts +8 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +10 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +74 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +11 -0
- package/dist/client/types.js.map +1 -0
- package/dist/hooks.d.ts +8 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +7 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/operations/OperationTypes.d.ts +239 -0
- package/dist/operations/OperationTypes.d.ts.map +1 -0
- package/dist/operations/OperationTypes.js +10 -0
- package/dist/operations/OperationTypes.js.map +1 -0
- package/dist/operations/OperationValidator.d.ts +32 -0
- package/dist/operations/OperationValidator.d.ts.map +1 -0
- package/dist/operations/OperationValidator.js +208 -0
- package/dist/operations/OperationValidator.js.map +1 -0
- package/dist/operations/apply/array.d.ts +22 -0
- package/dist/operations/apply/array.d.ts.map +1 -0
- package/dist/operations/apply/array.js +64 -0
- package/dist/operations/apply/array.js.map +1 -0
- package/dist/operations/apply/boolean.d.ts +18 -0
- package/dist/operations/apply/boolean.d.ts.map +1 -0
- package/dist/operations/apply/boolean.js +34 -0
- package/dist/operations/apply/boolean.js.map +1 -0
- package/dist/operations/apply/color.d.ts +14 -0
- package/dist/operations/apply/color.d.ts.map +1 -0
- package/dist/operations/apply/color.js +46 -0
- package/dist/operations/apply/color.js.map +1 -0
- package/dist/operations/apply/index.d.ts +18 -0
- package/dist/operations/apply/index.d.ts.map +1 -0
- package/dist/operations/apply/index.js +26 -0
- package/dist/operations/apply/index.js.map +1 -0
- package/dist/operations/apply/node.d.ts +24 -0
- package/dist/operations/apply/node.d.ts.map +1 -0
- package/dist/operations/apply/node.js +77 -0
- package/dist/operations/apply/node.js.map +1 -0
- package/dist/operations/apply/number.d.ts +26 -0
- package/dist/operations/apply/number.d.ts.map +1 -0
- package/dist/operations/apply/number.js +54 -0
- package/dist/operations/apply/number.js.map +1 -0
- package/dist/operations/apply/object.d.ts +14 -0
- package/dist/operations/apply/object.d.ts.map +1 -0
- package/dist/operations/apply/object.js +47 -0
- package/dist/operations/apply/object.js.map +1 -0
- package/dist/operations/apply/quaternion.d.ts +15 -0
- package/dist/operations/apply/quaternion.d.ts.map +1 -0
- package/dist/operations/apply/quaternion.js +33 -0
- package/dist/operations/apply/quaternion.js.map +1 -0
- package/dist/operations/apply/string.d.ts +14 -0
- package/dist/operations/apply/string.d.ts.map +1 -0
- package/dist/operations/apply/string.js +26 -0
- package/dist/operations/apply/string.js.map +1 -0
- package/dist/operations/apply/types.d.ts +34 -0
- package/dist/operations/apply/types.d.ts.map +1 -0
- package/dist/operations/apply/types.js +32 -0
- package/dist/operations/apply/types.js.map +1 -0
- package/dist/operations/apply/vector3.d.ts +18 -0
- package/dist/operations/apply/vector3.d.ts.map +1 -0
- package/dist/operations/apply/vector3.js +44 -0
- package/dist/operations/apply/vector3.js.map +1 -0
- package/dist/operations/dispatcher.d.ts +35 -0
- package/dist/operations/dispatcher.d.ts.map +1 -0
- package/dist/operations/dispatcher.js +107 -0
- package/dist/operations/dispatcher.js.map +1 -0
- package/dist/operations/index.d.ts +10 -0
- package/dist/operations/index.d.ts.map +1 -0
- package/dist/operations/index.js +17 -0
- package/dist/operations/index.js.map +1 -0
- package/dist/state/ConflictResolver.d.ts +36 -0
- package/dist/state/ConflictResolver.d.ts.map +1 -0
- package/dist/state/ConflictResolver.js +167 -0
- package/dist/state/ConflictResolver.js.map +1 -0
- package/dist/state/DType.d.ts +160 -0
- package/dist/state/DType.d.ts.map +1 -0
- package/dist/state/DType.js +282 -0
- package/dist/state/DType.js.map +1 -0
- package/dist/state/Schema.d.ts +32 -0
- package/dist/state/Schema.d.ts.map +1 -0
- package/dist/state/Schema.js +175 -0
- package/dist/state/Schema.js.map +1 -0
- package/dist/state/VectorClock.d.ts +42 -0
- package/dist/state/VectorClock.d.ts.map +1 -0
- package/dist/state/VectorClock.js +84 -0
- package/dist/state/VectorClock.js.map +1 -0
- package/dist/state/index.d.ts +11 -0
- package/dist/state/index.d.ts.map +1 -0
- package/dist/state/index.js +13 -0
- package/dist/state/index.js.map +1 -0
- package/docs/OPERATION_HINTS.md +222 -0
- package/docs/SCENE_GRAPH.md +373 -0
- package/docs/TYPE_BEHAVIORS.md +348 -0
- package/examples/01-basic-usage.ts +139 -0
- package/examples/02-concurrent-edits.ts +208 -0
- package/examples/03-scene-building.ts +258 -0
- package/examples/04-conflict-resolution.ts +339 -0
- package/examples/README.md +86 -0
- package/jest.config.js +19 -0
- package/package.json +57 -0
- package/src/client/EditBuffer.ts +105 -0
- package/src/client/actions.ts +397 -0
- package/src/client/createGraph.ts +132 -0
- package/src/client/hooks.tsx +249 -0
- package/src/client/index.ts +35 -0
- package/src/client/types.ts +94 -0
- package/src/hooks.ts +20 -0
- package/src/index.ts +14 -0
- package/src/operations/OperationTypes.ts +340 -0
- package/src/operations/OperationValidator.ts +260 -0
- package/src/operations/apply/array.ts +84 -0
- package/src/operations/apply/boolean.ts +48 -0
- package/src/operations/apply/color.ts +65 -0
- package/src/operations/apply/index.ts +37 -0
- package/src/operations/apply/node.ts +98 -0
- package/src/operations/apply/number.ts +76 -0
- package/src/operations/apply/object.ts +63 -0
- package/src/operations/apply/quaternion.ts +47 -0
- package/src/operations/apply/string.ts +36 -0
- package/src/operations/apply/types.ts +66 -0
- package/src/operations/apply/vector3.ts +60 -0
- package/src/operations/dispatcher.ts +127 -0
- package/src/operations/index.ts +80 -0
- package/src/state/ConflictResolver.ts +205 -0
- package/src/state/DType.ts +333 -0
- package/src/state/Schema.ts +236 -0
- package/src/state/VectorClock.ts +98 -0
- package/src/state/index.ts +14 -0
- package/tests/client/actions.test.ts +371 -0
- package/tests/client/edit-buffer.test.ts +117 -0
- package/tests/fixtures/array-ops.jsonl +6 -0
- package/tests/fixtures/boolean-ops.jsonl +6 -0
- package/tests/fixtures/color-ops.jsonl +4 -0
- package/tests/fixtures/edit-buffer.jsonl +3 -0
- package/tests/fixtures/node-ops.jsonl +6 -0
- package/tests/fixtures/number-ops.jsonl +7 -0
- package/tests/fixtures/object-ops.jsonl +4 -0
- package/tests/fixtures/operations.jsonl +7 -0
- package/tests/fixtures/string-ops.jsonl +4 -0
- package/tests/fixtures/undo-redo.jsonl +3 -0
- package/tests/fixtures/vector-ops.jsonl +9 -0
- package/tests/operations/collections.test.ts +193 -0
- package/tests/operations/nodes.test.ts +228 -0
- package/tests/operations/primitives.test.ts +222 -0
- package/tests/operations/vectors.test.ts +150 -0
- package/tsconfig.json +21 -0
- package/tsconfig.test.json +9 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for client actions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, beforeEach } from '@jest/globals';
|
|
6
|
+
import { readFileSync } from 'fs';
|
|
7
|
+
import { join, dirname } from 'path';
|
|
8
|
+
import { fileURLToPath } from 'url';
|
|
9
|
+
import {
|
|
10
|
+
createInitialState,
|
|
11
|
+
onEdit,
|
|
12
|
+
commitEdits,
|
|
13
|
+
cancelEdits,
|
|
14
|
+
undo,
|
|
15
|
+
redo,
|
|
16
|
+
onRemoteMessage,
|
|
17
|
+
onServerAck,
|
|
18
|
+
compact,
|
|
19
|
+
rebuildGraph,
|
|
20
|
+
} from '../../src/client/actions.js';
|
|
21
|
+
import type { ClientState } from '../../src/client/types.js';
|
|
22
|
+
import type { Operation, CRDTMessage } from '../../src/operations/OperationTypes.js';
|
|
23
|
+
|
|
24
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
|
|
26
|
+
// Helper to load JSONL fixtures
|
|
27
|
+
function loadFixtures<T>(filename: string): T[] {
|
|
28
|
+
const filepath = join(__dirname, '../fixtures', filename);
|
|
29
|
+
const content = readFileSync(filepath, 'utf-8');
|
|
30
|
+
return content
|
|
31
|
+
.trim()
|
|
32
|
+
.split('\n')
|
|
33
|
+
.map((line) => JSON.parse(line));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('Client Actions', () => {
|
|
37
|
+
let state: ClientState;
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
state = createInitialState('test-session');
|
|
41
|
+
// Add a test node (key=parent, path='children', value.key=new node)
|
|
42
|
+
state = onEdit(state, {
|
|
43
|
+
key: '', // No parent for root node
|
|
44
|
+
otype: 'node.insert',
|
|
45
|
+
path: 'children',
|
|
46
|
+
value: {
|
|
47
|
+
key: 'cube-1',
|
|
48
|
+
id: 'uuid-cube-1',
|
|
49
|
+
tag: 'Mesh',
|
|
50
|
+
name: 'Cube',
|
|
51
|
+
position: [0, 0, 0],
|
|
52
|
+
opacity: 1,
|
|
53
|
+
visible: true,
|
|
54
|
+
score: 0,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
const { state: committed } = commitEdits(state);
|
|
58
|
+
state = committed;
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('createInitialState', () => {
|
|
62
|
+
it('should create empty state with session ID', () => {
|
|
63
|
+
const s = createInitialState('my-session');
|
|
64
|
+
expect(s.sessionId).toBe('my-session');
|
|
65
|
+
expect(s.journal).toHaveLength(0);
|
|
66
|
+
expect(s.edits.ops).toHaveLength(0);
|
|
67
|
+
expect(s.lamportTime).toBe(0);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('onEdit', () => {
|
|
72
|
+
it('should add operation to edit buffer', () => {
|
|
73
|
+
const op: Operation = {
|
|
74
|
+
key: 'cube-1',
|
|
75
|
+
otype: 'number.set',
|
|
76
|
+
path: 'opacity',
|
|
77
|
+
value: 0.5,
|
|
78
|
+
};
|
|
79
|
+
const newState = onEdit(state, op);
|
|
80
|
+
expect(newState.edits.ops).toHaveLength(1);
|
|
81
|
+
expect(newState.edits.ops[0]).toEqual(op);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should apply operation to graph immediately', () => {
|
|
85
|
+
const op: Operation = {
|
|
86
|
+
key: 'cube-1',
|
|
87
|
+
otype: 'number.set',
|
|
88
|
+
path: 'opacity',
|
|
89
|
+
value: 0.5,
|
|
90
|
+
};
|
|
91
|
+
const newState = onEdit(state, op);
|
|
92
|
+
expect(newState.graph.nodes['cube-1']?.opacity).toBe(0.5);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should save start graph on first edit', () => {
|
|
96
|
+
const op: Operation = {
|
|
97
|
+
key: 'cube-1',
|
|
98
|
+
otype: 'number.set',
|
|
99
|
+
path: 'opacity',
|
|
100
|
+
value: 0.5,
|
|
101
|
+
};
|
|
102
|
+
const newState = onEdit(state, op);
|
|
103
|
+
expect(newState.edits.startGraph).not.toBeNull();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should merge additive operations', () => {
|
|
107
|
+
let s = state;
|
|
108
|
+
s = onEdit(s, {
|
|
109
|
+
key: 'cube-1',
|
|
110
|
+
otype: 'vector3.add',
|
|
111
|
+
path: 'position',
|
|
112
|
+
value: [1, 0, 0],
|
|
113
|
+
});
|
|
114
|
+
s = onEdit(s, {
|
|
115
|
+
key: 'cube-1',
|
|
116
|
+
otype: 'vector3.add',
|
|
117
|
+
path: 'position',
|
|
118
|
+
value: [0, 1, 0],
|
|
119
|
+
});
|
|
120
|
+
s = onEdit(s, {
|
|
121
|
+
key: 'cube-1',
|
|
122
|
+
otype: 'vector3.add',
|
|
123
|
+
path: 'position',
|
|
124
|
+
value: [0, 0, 1],
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Should merge into single op
|
|
128
|
+
expect(s.edits.ops).toHaveLength(1);
|
|
129
|
+
expect((s.edits.ops[0] as any).value).toEqual([1, 1, 1]);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('commitEdits', () => {
|
|
134
|
+
it('should create message from edit buffer', () => {
|
|
135
|
+
let s = onEdit(state, {
|
|
136
|
+
key: 'cube-1',
|
|
137
|
+
otype: 'number.set',
|
|
138
|
+
path: 'opacity',
|
|
139
|
+
value: 0.5,
|
|
140
|
+
});
|
|
141
|
+
const { state: newState, msg } = commitEdits(s);
|
|
142
|
+
|
|
143
|
+
expect(msg).not.toBeNull();
|
|
144
|
+
expect(msg!.sessionId).toBe('test-session');
|
|
145
|
+
expect(msg!.ops).toHaveLength(1);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should add message to journal', () => {
|
|
149
|
+
let s = onEdit(state, {
|
|
150
|
+
key: 'cube-1',
|
|
151
|
+
otype: 'number.set',
|
|
152
|
+
path: 'opacity',
|
|
153
|
+
value: 0.5,
|
|
154
|
+
});
|
|
155
|
+
const { state: newState } = commitEdits(s);
|
|
156
|
+
|
|
157
|
+
// Journal should have 2 entries (node.insert + opacity)
|
|
158
|
+
expect(newState.journal.length).toBeGreaterThan(state.journal.length);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should clear edit buffer after commit', () => {
|
|
162
|
+
let s = onEdit(state, {
|
|
163
|
+
key: 'cube-1',
|
|
164
|
+
otype: 'number.set',
|
|
165
|
+
path: 'opacity',
|
|
166
|
+
value: 0.5,
|
|
167
|
+
});
|
|
168
|
+
const { state: newState } = commitEdits(s);
|
|
169
|
+
|
|
170
|
+
expect(newState.edits.ops).toHaveLength(0);
|
|
171
|
+
expect(newState.edits.startGraph).toBeNull();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should return null message if no edits', () => {
|
|
175
|
+
const { state: newState, msg } = commitEdits(state);
|
|
176
|
+
expect(msg).toBeNull();
|
|
177
|
+
expect(newState).toBe(state);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('cancelEdits', () => {
|
|
182
|
+
it('should revert to start graph', () => {
|
|
183
|
+
const originalOpacity = state.graph.nodes['cube-1']?.opacity;
|
|
184
|
+
let s = onEdit(state, {
|
|
185
|
+
key: 'cube-1',
|
|
186
|
+
otype: 'number.set',
|
|
187
|
+
path: 'opacity',
|
|
188
|
+
value: 0.1,
|
|
189
|
+
});
|
|
190
|
+
expect(s.graph.nodes['cube-1']?.opacity).toBe(0.1);
|
|
191
|
+
|
|
192
|
+
s = cancelEdits(s);
|
|
193
|
+
expect(s.graph.nodes['cube-1']?.opacity).toBe(originalOpacity);
|
|
194
|
+
expect(s.edits.ops).toHaveLength(0);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe('undo/redo', () => {
|
|
199
|
+
it('should undo committed edit', () => {
|
|
200
|
+
const originalPosition = state.graph.nodes['cube-1']?.position;
|
|
201
|
+
|
|
202
|
+
let s = onEdit(state, {
|
|
203
|
+
key: 'cube-1',
|
|
204
|
+
otype: 'vector3.set',
|
|
205
|
+
path: 'position',
|
|
206
|
+
value: [5, 5, 5],
|
|
207
|
+
});
|
|
208
|
+
const { state: committed } = commitEdits(s);
|
|
209
|
+
s = committed;
|
|
210
|
+
|
|
211
|
+
expect(s.graph.nodes['cube-1']?.position).toEqual([5, 5, 5]);
|
|
212
|
+
|
|
213
|
+
const { state: undone } = undo(s);
|
|
214
|
+
expect(undone.graph.nodes['cube-1']?.position).toEqual(originalPosition);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should redo after undo', () => {
|
|
218
|
+
let s = onEdit(state, {
|
|
219
|
+
key: 'cube-1',
|
|
220
|
+
otype: 'vector3.set',
|
|
221
|
+
path: 'position',
|
|
222
|
+
value: [5, 5, 5],
|
|
223
|
+
});
|
|
224
|
+
const { state: committed } = commitEdits(s);
|
|
225
|
+
s = committed;
|
|
226
|
+
|
|
227
|
+
const { state: undone } = undo(s);
|
|
228
|
+
const { state: redone } = redo(undone);
|
|
229
|
+
|
|
230
|
+
expect(redone.graph.nodes['cube-1']?.position).toEqual([5, 5, 5]);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('should commit and undo uncommitted edits', () => {
|
|
234
|
+
const originalPosition = state.graph.nodes['cube-1']?.position;
|
|
235
|
+
|
|
236
|
+
let s = onEdit(state, {
|
|
237
|
+
key: 'cube-1',
|
|
238
|
+
otype: 'vector3.set',
|
|
239
|
+
path: 'position',
|
|
240
|
+
value: [5, 5, 5],
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// Undo without committing first - should commit then undo
|
|
244
|
+
const { state: undone, msg } = undo(s);
|
|
245
|
+
|
|
246
|
+
expect(msg).not.toBeNull();
|
|
247
|
+
expect(undone.graph.nodes['cube-1']?.position).toEqual(originalPosition);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
describe('onRemoteMessage', () => {
|
|
252
|
+
it('should apply remote message to graph', () => {
|
|
253
|
+
const remoteMsg: CRDTMessage = {
|
|
254
|
+
id: 'remote-1',
|
|
255
|
+
sessionId: 'other-session',
|
|
256
|
+
clock: { 'other-session': 1 },
|
|
257
|
+
lamportTime: 10,
|
|
258
|
+
timestamp: Date.now(),
|
|
259
|
+
ops: [
|
|
260
|
+
{
|
|
261
|
+
key: 'cube-1',
|
|
262
|
+
otype: 'number.set',
|
|
263
|
+
path: 'opacity',
|
|
264
|
+
value: 0.3,
|
|
265
|
+
},
|
|
266
|
+
],
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const newState = onRemoteMessage(state, remoteMsg);
|
|
270
|
+
expect(newState.graph.nodes['cube-1']?.opacity).toBe(0.3);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('should skip duplicate messages', () => {
|
|
274
|
+
const remoteMsg: CRDTMessage = {
|
|
275
|
+
id: 'remote-1',
|
|
276
|
+
sessionId: 'other-session',
|
|
277
|
+
clock: { 'other-session': 1 },
|
|
278
|
+
lamportTime: 10,
|
|
279
|
+
timestamp: Date.now(),
|
|
280
|
+
ops: [
|
|
281
|
+
{
|
|
282
|
+
key: 'cube-1',
|
|
283
|
+
otype: 'number.set',
|
|
284
|
+
path: 'opacity',
|
|
285
|
+
value: 0.3,
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
let s = onRemoteMessage(state, remoteMsg);
|
|
291
|
+
const journalLength = s.journal.length;
|
|
292
|
+
|
|
293
|
+
s = onRemoteMessage(s, remoteMsg);
|
|
294
|
+
expect(s.journal.length).toBe(journalLength);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('should handle remote undo', () => {
|
|
298
|
+
// First add a message
|
|
299
|
+
let s = onEdit(state, {
|
|
300
|
+
key: 'cube-1',
|
|
301
|
+
otype: 'vector3.set',
|
|
302
|
+
path: 'position',
|
|
303
|
+
value: [5, 5, 5],
|
|
304
|
+
});
|
|
305
|
+
const { state: committed, msg } = commitEdits(s);
|
|
306
|
+
s = onServerAck(committed, msg!.id);
|
|
307
|
+
|
|
308
|
+
expect(s.graph.nodes['cube-1']?.position).toEqual([5, 5, 5]);
|
|
309
|
+
|
|
310
|
+
// Receive remote undo
|
|
311
|
+
const undoMsg: CRDTMessage = {
|
|
312
|
+
id: 'undo-1',
|
|
313
|
+
sessionId: 'other-session',
|
|
314
|
+
clock: { 'other-session': 1 },
|
|
315
|
+
lamportTime: 20,
|
|
316
|
+
timestamp: Date.now(),
|
|
317
|
+
ops: [
|
|
318
|
+
{
|
|
319
|
+
key: '_meta',
|
|
320
|
+
otype: 'meta.undo',
|
|
321
|
+
path: '_meta',
|
|
322
|
+
targetMsgId: msg!.id,
|
|
323
|
+
},
|
|
324
|
+
],
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const undone = onRemoteMessage(s, undoMsg);
|
|
328
|
+
// Position should revert to original
|
|
329
|
+
expect(undone.graph.nodes['cube-1']?.position).toEqual([0, 0, 0]);
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
describe('onServerAck', () => {
|
|
334
|
+
it('should mark message as acknowledged', () => {
|
|
335
|
+
let s = onEdit(state, {
|
|
336
|
+
key: 'cube-1',
|
|
337
|
+
otype: 'number.set',
|
|
338
|
+
path: 'opacity',
|
|
339
|
+
value: 0.5,
|
|
340
|
+
});
|
|
341
|
+
const { state: committed, msg } = commitEdits(s);
|
|
342
|
+
s = committed;
|
|
343
|
+
|
|
344
|
+
const lastEntry = s.journal[s.journal.length - 1];
|
|
345
|
+
expect(lastEntry.ack).toBe(false);
|
|
346
|
+
|
|
347
|
+
s = onServerAck(s, msg!.id);
|
|
348
|
+
const ackedEntry = s.journal.find((e) => e.msg.id === msg!.id);
|
|
349
|
+
expect(ackedEntry?.ack).toBe(true);
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
describe('compact', () => {
|
|
354
|
+
it('should bake acknowledged entries into snapshot', () => {
|
|
355
|
+
let s = onEdit(state, {
|
|
356
|
+
key: 'cube-1',
|
|
357
|
+
otype: 'number.set',
|
|
358
|
+
path: 'opacity',
|
|
359
|
+
value: 0.5,
|
|
360
|
+
});
|
|
361
|
+
const { state: committed, msg } = commitEdits(s);
|
|
362
|
+
s = onServerAck(committed, msg!.id);
|
|
363
|
+
|
|
364
|
+
const journalLengthBefore = s.journal.length;
|
|
365
|
+
s = compact(s);
|
|
366
|
+
|
|
367
|
+
expect(s.journal.length).toBeLessThan(journalLengthBefore);
|
|
368
|
+
expect(s.snapshot.graph.nodes['cube-1']?.opacity).toBe(0.5);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for EditBuffer merging logic
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect } from '@jest/globals';
|
|
6
|
+
import { readFileSync } from 'fs';
|
|
7
|
+
import { join, dirname } from 'path';
|
|
8
|
+
import { fileURLToPath } from 'url';
|
|
9
|
+
import { isAdditiveOp, mergeValues, EditBufferImpl } from '../../src/client/EditBuffer.js';
|
|
10
|
+
import type { Operation } from '../../src/operations/OperationTypes.js';
|
|
11
|
+
|
|
12
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
|
|
14
|
+
interface EditBufferFixture {
|
|
15
|
+
name: string;
|
|
16
|
+
ops: Operation[];
|
|
17
|
+
expected_merged?: any;
|
|
18
|
+
expected_count?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function loadFixtures(): EditBufferFixture[] {
|
|
22
|
+
const filepath = join(__dirname, '../fixtures/edit-buffer.jsonl');
|
|
23
|
+
const content = readFileSync(filepath, 'utf-8');
|
|
24
|
+
return content
|
|
25
|
+
.trim()
|
|
26
|
+
.split('\n')
|
|
27
|
+
.map((line) => JSON.parse(line));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('EditBuffer', () => {
|
|
31
|
+
describe('isAdditiveOp', () => {
|
|
32
|
+
it('should identify additive operations', () => {
|
|
33
|
+
expect(isAdditiveOp('vector3.add')).toBe(true);
|
|
34
|
+
expect(isAdditiveOp('number.add')).toBe(true);
|
|
35
|
+
expect(isAdditiveOp('quaternion.multiply')).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should identify non-additive operations', () => {
|
|
39
|
+
expect(isAdditiveOp('vector3.set')).toBe(false);
|
|
40
|
+
expect(isAdditiveOp('number.set')).toBe(false);
|
|
41
|
+
expect(isAdditiveOp('boolean.set')).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('mergeValues', () => {
|
|
46
|
+
it('should merge vector3.add values', () => {
|
|
47
|
+
const result = mergeValues('vector3.add', [1, 0, 0], [0, 1, 0]);
|
|
48
|
+
expect(result).toEqual([1, 1, 0]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should merge number.add values', () => {
|
|
52
|
+
const result = mergeValues('number.add', 10, 5);
|
|
53
|
+
expect(result).toBe(15);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should replace non-additive values', () => {
|
|
57
|
+
const result = mergeValues('number.set', 10, 20);
|
|
58
|
+
expect(result).toBe(20);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('EditBufferImpl', () => {
|
|
63
|
+
it('should merge additive ops from fixture', () => {
|
|
64
|
+
const fixtures = loadFixtures();
|
|
65
|
+
const mergeFixture = fixtures.find((f) => f.name === 'merge_additive_ops');
|
|
66
|
+
|
|
67
|
+
const buffer = new EditBufferImpl();
|
|
68
|
+
for (const op of mergeFixture!.ops) {
|
|
69
|
+
buffer.add(op);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const ops = buffer.getOps();
|
|
73
|
+
expect(ops).toHaveLength(1);
|
|
74
|
+
expect((ops[0] as any).value).toEqual(mergeFixture!.expected_merged.value);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should replace LWW ops from fixture', () => {
|
|
78
|
+
const fixtures = loadFixtures();
|
|
79
|
+
const replaceFixture = fixtures.find((f) => f.name === 'replace_lww_ops');
|
|
80
|
+
|
|
81
|
+
const buffer = new EditBufferImpl();
|
|
82
|
+
for (const op of replaceFixture!.ops) {
|
|
83
|
+
buffer.add(op);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const ops = buffer.getOps();
|
|
87
|
+
expect(ops).toHaveLength(1);
|
|
88
|
+
expect((ops[0] as any).value).toBe(replaceFixture!.expected_merged.value);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should handle multiple properties from fixture', () => {
|
|
92
|
+
const fixtures = loadFixtures();
|
|
93
|
+
const multiFixture = fixtures.find((f) => f.name === 'multiple_properties');
|
|
94
|
+
|
|
95
|
+
const buffer = new EditBufferImpl();
|
|
96
|
+
for (const op of multiFixture!.ops) {
|
|
97
|
+
buffer.add(op);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const ops = buffer.getOps();
|
|
101
|
+
expect(ops).toHaveLength(multiFixture!.expected_count!);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should clear buffer', () => {
|
|
105
|
+
const buffer = new EditBufferImpl();
|
|
106
|
+
buffer.add({
|
|
107
|
+
key: 'cube-1',
|
|
108
|
+
otype: 'number.set',
|
|
109
|
+
path: 'opacity',
|
|
110
|
+
value: 0.5,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
buffer.clear();
|
|
114
|
+
expect(buffer.getOps()).toHaveLength(0);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{"name": "array_set", "op": {"key": "node-1", "otype": "array.set", "path": "tags", "value": ["enemy", "active"]}}
|
|
2
|
+
{"name": "array_set_empty", "op": {"key": "node-1", "otype": "array.set", "path": "tags", "value": []}}
|
|
3
|
+
{"name": "array_push", "op": {"key": "node-1", "otype": "array.push", "path": "tags", "value": "new-tag"}}
|
|
4
|
+
{"name": "array_push_number", "op": {"key": "node-1", "otype": "array.push", "path": "scores", "value": 100}}
|
|
5
|
+
{"name": "array_union", "op": {"key": "node-1", "otype": "array.union", "path": "tags", "value": ["tag-a", "tag-b"]}}
|
|
6
|
+
{"name": "array_remove", "op": {"key": "node-1", "otype": "array.remove", "path": "tags", "value": "enemy"}}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{"name": "boolean_set_true", "op": {"key": "node-1", "otype": "boolean.set", "path": "visible", "value": true}}
|
|
2
|
+
{"name": "boolean_set_false", "op": {"key": "node-1", "otype": "boolean.set", "path": "visible", "value": false}}
|
|
3
|
+
{"name": "boolean_or_true", "op": {"key": "node-1", "otype": "boolean.or", "path": "enabled", "value": true}}
|
|
4
|
+
{"name": "boolean_or_false", "op": {"key": "node-1", "otype": "boolean.or", "path": "enabled", "value": false}}
|
|
5
|
+
{"name": "boolean_and_true", "op": {"key": "node-1", "otype": "boolean.and", "path": "active", "value": true}}
|
|
6
|
+
{"name": "boolean_and_false", "op": {"key": "node-1", "otype": "boolean.and", "path": "active", "value": false}}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
{"name": "color_set_red", "op": {"key": "node-1", "otype": "color.set", "path": "color", "value": "#ff0000"}}
|
|
2
|
+
{"name": "color_set_green", "op": {"key": "node-1", "otype": "color.set", "path": "color", "value": "#00ff00"}}
|
|
3
|
+
{"name": "color_set_rgb", "op": {"key": "node-1", "otype": "color.set", "path": "color", "value": "#336699"}}
|
|
4
|
+
{"name": "color_blend", "op": {"key": "node-1", "otype": "color.blend", "path": "color", "value": "#ffffff"}}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
{"name": "merge_additive_ops", "ops": [{"key": "cube-1", "otype": "vector3.add", "path": "position", "value": [1, 0, 0]}, {"key": "cube-1", "otype": "vector3.add", "path": "position", "value": [0, 1, 0]}, {"key": "cube-1", "otype": "vector3.add", "path": "position", "value": [0, 0, 1]}], "expected_merged": {"key": "cube-1", "otype": "vector3.add", "path": "position", "value": [1, 1, 1]}}
|
|
2
|
+
{"name": "replace_lww_ops", "ops": [{"key": "cube-1", "otype": "number.set", "path": "opacity", "value": 0.3}, {"key": "cube-1", "otype": "number.set", "path": "opacity", "value": 0.7}], "expected_merged": {"key": "cube-1", "otype": "number.set", "path": "opacity", "value": 0.7}}
|
|
3
|
+
{"name": "multiple_properties", "ops": [{"key": "cube-1", "otype": "vector3.add", "path": "position", "value": [1, 0, 0]}, {"key": "cube-1", "otype": "number.set", "path": "opacity", "value": 0.5}], "expected_count": 2}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{"name": "node_insert_root", "op": {"key": "", "otype": "node.insert", "path": "children", "value": {"key": "scene", "id": "uuid-scene", "tag": "Scene", "name": "Root Scene"}}}
|
|
2
|
+
{"name": "node_insert_mesh", "op": {"key": "scene", "otype": "node.insert", "path": "children", "value": {"key": "cube-1", "id": "uuid-cube-1", "tag": "Mesh", "name": "Cube", "position": [0, 0, 0], "scale": [1, 1, 1]}}}
|
|
3
|
+
{"name": "node_insert_group", "op": {"key": "scene", "otype": "node.insert", "path": "children", "value": {"key": "group-1", "id": "uuid-group-1", "tag": "Group", "name": "Group"}}}
|
|
4
|
+
{"name": "node_insert_nested", "op": {"key": "group-1", "otype": "node.insert", "path": "children", "value": {"key": "sphere-1", "id": "uuid-sphere-1", "tag": "Mesh", "name": "Sphere"}}}
|
|
5
|
+
{"name": "node_remove", "op": {"key": "scene", "otype": "node.remove", "path": "children", "value": "cube-1"}}
|
|
6
|
+
{"name": "node_remove_nested", "op": {"key": "group-1", "otype": "node.remove", "path": "children", "value": "sphere-1"}}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{"name": "number_set", "op": {"key": "node-1", "otype": "number.set", "path": "opacity", "value": 0.5}}
|
|
2
|
+
{"name": "number_add", "op": {"key": "node-1", "otype": "number.add", "path": "score", "value": 10}}
|
|
3
|
+
{"name": "number_multiply", "op": {"key": "node-1", "otype": "number.multiply", "path": "scale", "value": 2}}
|
|
4
|
+
{"name": "number_min", "op": {"key": "node-1", "otype": "number.min", "path": "health", "value": 50}}
|
|
5
|
+
{"name": "number_max", "op": {"key": "node-1", "otype": "number.max", "path": "damage", "value": 25}}
|
|
6
|
+
{"name": "number_add_negative", "op": {"key": "node-1", "otype": "number.add", "path": "score", "value": -5}}
|
|
7
|
+
{"name": "number_multiply_fraction", "op": {"key": "node-1", "otype": "number.multiply", "path": "scale", "value": 0.5}}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
{"name": "object_set", "op": {"key": "node-1", "otype": "object.set", "path": "metadata", "value": {"author": "user-1", "version": 1}}}
|
|
2
|
+
{"name": "object_set_empty", "op": {"key": "node-1", "otype": "object.set", "path": "metadata", "value": {}}}
|
|
3
|
+
{"name": "object_merge", "op": {"key": "node-1", "otype": "object.merge", "path": "metadata", "value": {"tags": ["a", "b"]}}}
|
|
4
|
+
{"name": "object_merge_nested", "op": {"key": "node-1", "otype": "object.merge", "path": "config", "value": {"settings": {"enabled": true}}}}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{"name": "number.set", "op": {"key": "cube-1", "otype": "number.set", "path": "opacity", "value": 0.5}, "expected": {"opacity": 0.5}}
|
|
2
|
+
{"name": "number.add", "op": {"key": "cube-1", "otype": "number.add", "path": "score", "value": 10}, "expected": {"score": 10}}
|
|
3
|
+
{"name": "vector3.set", "op": {"key": "cube-1", "otype": "vector3.set", "path": "position", "value": [1, 2, 3]}, "expected": {"position": [1, 2, 3]}}
|
|
4
|
+
{"name": "vector3.add", "op": {"key": "cube-1", "otype": "vector3.add", "path": "position", "value": [0.1, 0.2, 0.3]}, "initial": {"position": [1, 2, 3]}, "expected": {"position": [1.1, 2.2, 3.3]}}
|
|
5
|
+
{"name": "boolean.set", "op": {"key": "cube-1", "otype": "boolean.set", "path": "visible", "value": false}, "expected": {"visible": false}}
|
|
6
|
+
{"name": "string.set", "op": {"key": "cube-1", "otype": "string.set", "path": "name", "value": "MyCube"}, "expected": {"name": "MyCube"}}
|
|
7
|
+
{"name": "node.insert", "op": {"key": "sphere-1", "otype": "node.insert", "path": "sphere-1", "value": {"id": "uuid-sphere", "tag": "Mesh", "name": "Sphere"}}, "expected": {"nodes": {"sphere-1": {"id": "uuid-sphere", "tag": "Mesh", "name": "Sphere"}}}}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
{"name": "string_set", "op": {"key": "node-1", "otype": "string.set", "path": "name", "value": "Player"}}
|
|
2
|
+
{"name": "string_set_empty", "op": {"key": "node-1", "otype": "string.set", "path": "name", "value": ""}}
|
|
3
|
+
{"name": "string_concat", "op": {"key": "node-1", "otype": "string.concat", "path": "tags", "value": "active"}}
|
|
4
|
+
{"name": "string_concat_separator", "op": {"key": "node-1", "otype": "string.concat", "path": "tags", "value": "enemy", "separator": ","}}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
{"name": "undo_single_edit", "steps": [{"action": "edit", "op": {"key": "cube-1", "otype": "vector3.set", "path": "position", "value": [5, 5, 5]}}, {"action": "commit"}, {"action": "undo"}], "expected": {"position": [0, 0, 0]}}
|
|
2
|
+
{"name": "redo_after_undo", "steps": [{"action": "edit", "op": {"key": "cube-1", "otype": "vector3.set", "path": "position", "value": [5, 5, 5]}}, {"action": "commit"}, {"action": "undo"}, {"action": "redo"}], "expected": {"position": [5, 5, 5]}}
|
|
3
|
+
{"name": "undo_uncommitted", "steps": [{"action": "edit", "op": {"key": "cube-1", "otype": "vector3.set", "path": "position", "value": [5, 5, 5]}}, {"action": "undo"}], "expected": {"position": [0, 0, 0]}}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{"name": "vector3_set", "op": {"key": "node-1", "otype": "vector3.set", "path": "position", "value": [1, 2, 3]}}
|
|
2
|
+
{"name": "vector3_set_zero", "op": {"key": "node-1", "otype": "vector3.set", "path": "position", "value": [0, 0, 0]}}
|
|
3
|
+
{"name": "vector3_add", "op": {"key": "node-1", "otype": "vector3.add", "path": "position", "value": [0.5, 0.5, 0.5]}}
|
|
4
|
+
{"name": "vector3_add_negative", "op": {"key": "node-1", "otype": "vector3.add", "path": "position", "value": [-1, -1, -1]}}
|
|
5
|
+
{"name": "vector3_multiply", "op": {"key": "node-1", "otype": "vector3.multiply", "path": "scale", "value": [2, 2, 2]}}
|
|
6
|
+
{"name": "vector3_multiply_non_uniform", "op": {"key": "node-1", "otype": "vector3.multiply", "path": "scale", "value": [1, 2, 3]}}
|
|
7
|
+
{"name": "quaternion_set", "op": {"key": "node-1", "otype": "quaternion.set", "path": "rotation", "value": [0, 0, 0, 1]}}
|
|
8
|
+
{"name": "quaternion_set_rotated", "op": {"key": "node-1", "otype": "quaternion.set", "path": "rotation", "value": [0, 0.7071, 0, 0.7071]}}
|
|
9
|
+
{"name": "quaternion_multiply", "op": {"key": "node-1", "otype": "quaternion.multiply", "path": "rotation", "value": [0, 0.3827, 0, 0.9239]}}
|