Skip to content

Commit f20c7ae

Browse files
committed
test(json-crdt): 💍 add "upd_arr" operation to fuzz testing
1 parent 120b790 commit f20c7ae

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/json-crdt/__tests__/fuzzer/Picker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {DelOp, InsObjOp, InsStrOp, InsBinOp, InsArrOp} from '../../../json-crdt-patch/operations';
1+
import {DelOp, InsObjOp, InsStrOp, InsBinOp, InsArrOp, UpdArrOp} from '../../../json-crdt-patch/operations';
22
import {RandomJson} from '@jsonjoy.com/util/lib/json-random';
33
import type {JsonNode, ObjNode, ArrNode, BinNode, StrNode} from '../../nodes';
44
import type {Model} from '../../model/Model';
@@ -7,7 +7,7 @@ import type {FuzzerOptions} from './types';
77

88
type StringOp = typeof InsStrOp | typeof DelOp;
99
type BinaryOp = typeof InsBinOp | typeof DelOp;
10-
type ArrayOp = typeof InsArrOp | typeof DelOp;
10+
type ArrayOp = typeof InsArrOp | typeof DelOp | typeof UpdArrOp;
1111
type ObjectOp = typeof InsObjOp | typeof DelOp;
1212

1313
const commonKeys = ['a', 'op', 'test', 'name', '', '__proto__'];
@@ -56,6 +56,7 @@ export class Picker {
5656
public pickArrayOperation(node: ArrNode): ArrayOp {
5757
if (!node.length()) return InsArrOp;
5858
if (Math.random() > 0.45) return InsArrOp;
59+
if (Math.random() > 0.45) return UpdArrOp;
5960
else return DelOp;
6061
}
6162

src/json-crdt/__tests__/fuzzer/SessionLogical.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {decode as decodeJson} from '../../../json-crdt-patch/codec/verbose/decod
44
import {Decoder as BinaryDecoder} from '../../codec/structural/binary/Decoder';
55
import {Decoder as CompactDecoder} from '../../codec/structural/compact/Decoder';
66
import {Decoder as JsonDecoder} from '../../codec/structural/verbose/Decoder';
7-
import {DelOp, InsObjOp, InsStrOp, InsBinOp, InsArrOp} from '../../../json-crdt-patch/operations';
7+
import {DelOp, InsObjOp, InsStrOp, InsBinOp, InsArrOp, UpdArrOp} from '../../../json-crdt-patch/operations';
88
import {encode as encodeCompact} from '../../../json-crdt-patch/codec/compact/encode';
99
import {encode as encodeJson} from '../../../json-crdt-patch/codec/verbose/encode';
1010
import {Encoder as BinaryEncoder} from '../../codec/structural/binary/Encoder';
@@ -161,7 +161,7 @@ export class SessionLogical {
161161
const opcode = this.fuzzer.picker.pickArrayOperation(node);
162162
const builder = new PatchBuilder(model.clock);
163163
const length = node.length();
164-
if (opcode === InsArrOp) {
164+
if (!length || (opcode === InsArrOp)) {
165165
const json = RandomJson.generate({nodeCount: Math.ceil(Math.random() * 5)});
166166
const valueId = builder.json(json);
167167
if (!length) builder.insArr(node.id, node.id, [valueId]);
@@ -173,6 +173,14 @@ export class SessionLogical {
173173
builder.insArr(node.id, afterId, [valueId]);
174174
}
175175
}
176+
} else if (opcode === UpdArrOp) {
177+
const pos = Math.floor(Math.random() * length);
178+
const keyId = node.find(pos);
179+
if (keyId) {
180+
const json = RandomJson.generate({nodeCount: Math.ceil(Math.random() * 5)});
181+
const valueId = builder.json(json);
182+
builder.updArr(node.id, keyId, valueId);
183+
}
176184
} else {
177185
if (!length) return builder.patch;
178186
const pos = Math.floor(Math.random() * length);

0 commit comments

Comments
 (0)