Skip to content

Commit 4c06bd5

Browse files
committed
test(json-crdt-patch): 💍 add schema builder smoke test
1 parent 2b29cb4 commit 4c06bd5

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

packages/json-joy/src/json-crdt-patch/__tests__/schema.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {Model} from '../../json-crdt/model';
22
import {type NodeBuilder, s} from '../schema';
33

4+
const escapeTerminalCtrlChars = (str: string): string =>
5+
str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
6+
47
describe('nodes', () => {
58
describe('obj', () => {
69
test('can create basic "obj" schema', () => {
@@ -17,6 +20,67 @@ describe('nodes', () => {
1720
});
1821
});
1922

23+
test('can print schema', () => {
24+
const schema = s.obj(
25+
{
26+
name: s.str('abc'),
27+
age: s.con(123),
28+
data: s.bin(new Uint8Array([0xff, 0x00, 0x44, 0x55])),
29+
important: s.val(s.con(null)),
30+
undefined: s.con(undefined),
31+
coords: s.vec(s.con(1.1232), s.con(2.1232)),
32+
tags: s.arr([s.str('tag1'), s.str('tag2')]),
33+
extension: s.ext(
34+
5,
35+
s.obj({
36+
foo: s.con('bar'),
37+
}),
38+
),
39+
},
40+
{
41+
verified: s.con(true),
42+
signature: s.con(new Uint8Array([1, 2, 3])),
43+
label: s.con('x78398ks-asdf'),
44+
},
45+
);
46+
const str = schema + '';
47+
expect('\n' + escapeTerminalCtrlChars(str)).toBe(`
48+
obj
49+
├─ "name"
50+
│ └─ str { "abc" }
51+
├─ "age"
52+
│ └─ con { 123 }
53+
├─ "data"
54+
│ └─ bin { 255, 0, 68, 85 }
55+
├─ "important"
56+
│ └─ val
57+
│ └─ con { !n }
58+
├─ "undefined"
59+
│ └─ con { !u }
60+
├─ "coords"
61+
│ └─ vec
62+
│ ├─ 0: con { 1.1232 }
63+
│ └─ 1: con { 2.1232 }
64+
├─ "tags"
65+
│ └─ arr
66+
│ ├─ [0]: str { "tag1" }
67+
│ └─ [1]: str { "tag2" }
68+
├─ "extension"
69+
│ └─ ext(5)
70+
│ └─ obj
71+
│ └─ "foo"
72+
│ └─ con { "bar" }
73+
├─ "verified"?
74+
│ └─ con { !t }
75+
├─ "signature"?
76+
│ └─ con Uint8Array { 1, 2, 3 }
77+
└─ "label"?
78+
└─ con { "x78398ks-asdf" }`);
79+
// console.log(str);
80+
// const model = Model.create(schema);
81+
// console.log(model.root.child() + '');
82+
});
83+
2084
describe('json', () => {
2185
const assertSchemasEqual = <A extends NodeBuilder, B extends A>(schema1: A, schema2: B): void => {
2286
const model1 = Model.create(schema1, 123456789);

0 commit comments

Comments
 (0)