-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcodec.test.ts
More file actions
313 lines (272 loc) Β· 9.8 KB
/
Copy pathcodec.test.ts
File metadata and controls
313 lines (272 loc) Β· 9.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* Backwards compatibility tests for codec message round-trips.
*
* These tests verify that messages encoded with the legacy TypeBox (0.34.x)
* can be decoded and validated by the new TypeBox (1.0), and vice versa.
* This ensures that during a rolling upgrade, servers/clients using different
* river versions can communicate.
*/
import { describe, test, expect } from 'vitest';
import { Type as LegacyType } from 'legacyTypebox';
import { Value as LegacyValue } from 'legacyTypebox/value';
import { Type as NewType } from 'typebox';
import { Value as NewValue } from 'typebox/value';
import { NaiveJsonCodec, BinaryCodec, CodecMessageAdapter } from '../../codec';
import {
OpaqueTransportMessageSchema,
type OpaqueTransportMessage,
} from '../../transport/message';
import { Uint8ArrayType } from '../../customSchemas';
function makeTransportMessage(
payload: unknown,
overrides: Partial<OpaqueTransportMessage> = {},
): OpaqueTransportMessage {
return {
id: 'msg-1',
from: 'client-1',
to: 'server-1',
seq: 0,
ack: 0,
streamId: 'stream-1',
controlFlags: 0,
payload,
...overrides,
};
}
const LegacyOpaqueTransportMessageSchema = LegacyType.Object({
id: LegacyType.String(),
from: LegacyType.String(),
to: LegacyType.String(),
seq: LegacyType.Integer(),
ack: LegacyType.Integer(),
serviceName: LegacyType.Optional(LegacyType.String()),
procedureName: LegacyType.Optional(LegacyType.String()),
streamId: LegacyType.String(),
controlFlags: LegacyType.Integer(),
tracing: LegacyType.Optional(
LegacyType.Object({
traceparent: LegacyType.String(),
tracestate: LegacyType.String(),
}),
),
payload: LegacyType.Unknown(),
});
describe.each([
{ name: 'naive JSON codec', codec: NaiveJsonCodec },
{ name: 'binary codec', codec: BinaryCodec },
])('codec backwards compatibility ($name)', ({ codec }) => {
const adapter = new CodecMessageAdapter(codec);
describe('basic message round-trip', () => {
test('message with object payload survives encode/decode', () => {
const msg = makeTransportMessage({ greeting: 'hello', count: 42 });
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
expect(decoded.value).toEqual(msg);
});
test('message with nested result payload', () => {
const msg = makeTransportMessage({
ok: true,
payload: { result: 42 },
});
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
expect(decoded.value).toEqual(msg);
});
test('message with error payload (Err result format)', () => {
const msg = makeTransportMessage({
ok: false,
payload: {
code: 'SOME_ERROR',
message: 'something went wrong',
extras: { detail: 'extra info' },
},
});
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
expect(decoded.value).toEqual(msg);
});
});
describe('Uint8Array payload handling', () => {
test('message with Uint8Array in payload survives round-trip', () => {
const bytes = new Uint8Array([72, 101, 108, 108, 111]);
const msg = makeTransportMessage({
ok: true,
payload: { contents: bytes },
});
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
const decodedPayload = decoded.value.payload as {
ok: boolean;
payload: { contents: Uint8Array };
};
expect(decodedPayload.ok).toBe(true);
expect(new Uint8Array(decodedPayload.payload.contents)).toEqual(bytes);
});
});
describe('cross-version message validation', () => {
test('encoded message passes new schema validation', () => {
const msg = makeTransportMessage({ ok: true, payload: { result: 1 } });
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
expect(NewValue.Check(OpaqueTransportMessageSchema, decoded.value)).toBe(
true,
);
});
test('encoded message passes legacy schema validation', () => {
const msg = makeTransportMessage({ ok: true, payload: { result: 1 } });
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
expect(
LegacyValue.Check(LegacyOpaqueTransportMessageSchema, decoded.value),
).toBe(true);
});
});
describe('cross-version payload validation', () => {
test('object validated by legacy TypeBox is also valid under new TypeBox', () => {
const legacySchema = LegacyType.Object({
name: LegacyType.String(),
age: LegacyType.Number(),
});
const newSchema = NewType.Object({
name: NewType.String(),
age: NewType.Number(),
});
const data = { name: 'Alice', age: 30 };
expect(LegacyValue.Check(legacySchema, data)).toBe(true);
expect(NewValue.Check(newSchema, data)).toBe(true);
});
test('union validated by legacy TypeBox is also valid under new TypeBox', () => {
const legacySchema = LegacyType.Union([
LegacyType.Object({
code: LegacyType.Literal('ERR_A'),
message: LegacyType.String(),
}),
LegacyType.Object({
code: LegacyType.Literal('ERR_B'),
message: LegacyType.String(),
extras: LegacyType.Object({ detail: LegacyType.String() }),
}),
]);
const newSchema = NewType.Union([
NewType.Object({
code: NewType.Literal('ERR_A'),
message: NewType.String(),
}),
NewType.Object({
code: NewType.Literal('ERR_B'),
message: NewType.String(),
extras: NewType.Object({ detail: NewType.String() }),
}),
]);
const data1 = { code: 'ERR_A', message: 'oops' };
const data2 = {
code: 'ERR_B',
message: 'oops',
extras: { detail: 'info' },
};
const invalidData = { code: 'ERR_C', message: 'unknown' };
expect(LegacyValue.Check(legacySchema, data1)).toBe(true);
expect(NewValue.Check(newSchema, data1)).toBe(true);
expect(LegacyValue.Check(legacySchema, data2)).toBe(true);
expect(NewValue.Check(newSchema, data2)).toBe(true);
expect(LegacyValue.Check(legacySchema, invalidData)).toBe(false);
expect(NewValue.Check(newSchema, invalidData)).toBe(false);
});
test('Uint8Array validated by legacy Type.Uint8Array matches new Uint8ArrayType', () => {
const legacySchema = LegacyType.Uint8Array();
const newSchema = Uint8ArrayType();
const validData = new Uint8Array([1, 2, 3]);
expect(LegacyValue.Check(legacySchema, validData)).toBe(true);
expect(NewValue.Check(newSchema, validData)).toBe(true);
expect(LegacyValue.Check(legacySchema, [1, 2, 3])).toBe(false);
expect(NewValue.Check(newSchema, [1, 2, 3])).toBe(false);
expect(LegacyValue.Check(legacySchema, 'not bytes')).toBe(false);
expect(NewValue.Check(newSchema, 'not bytes')).toBe(false);
});
});
describe('full transport message round-trip with validation', () => {
test('encode with new TypeBox, validate with legacy', () => {
const msg = makeTransportMessage(
{ ok: true, payload: { name: 'test', value: 42 } },
{
serviceName: 'myService',
procedureName: 'myProcedure',
controlFlags: 1,
},
);
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
expect(
LegacyValue.Check(LegacyOpaqueTransportMessageSchema, decoded.value),
).toBe(true);
expect(NewValue.Check(OpaqueTransportMessageSchema, decoded.value)).toBe(
true,
);
});
test('handshake request message round-trip', () => {
const msg = makeTransportMessage(
{
type: 'HANDSHAKE_REQ',
protocolVersion: 'v2.0',
sessionId: 'session-1',
expectedSessionState: {
nextExpectedSeq: 0,
nextSentSeq: 0,
},
},
{ controlFlags: 1 },
);
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
expect(decoded.value).toEqual(msg);
});
test('handshake response message round-trip', () => {
const msg = makeTransportMessage(
{
type: 'HANDSHAKE_RESP',
status: { ok: true, sessionId: 'session-123' },
},
{ controlFlags: 1 },
);
const encoded = adapter.toBuffer(msg);
expect(encoded.ok).toBe(true);
if (!encoded.ok) return;
const decoded = adapter.fromBuffer(encoded.value);
expect(decoded.ok).toBe(true);
if (!decoded.ok) return;
expect(decoded.value).toEqual(msg);
});
});
});