-
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
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import { describe, expect, test } from "bun:test";
import { create, type MessageInitShape } from "@bufbuild/protobuf";
import { DateSchema } from "@yamori/proto/yamori/type/v1/date_pb.js";
import { isSameBytes, maskMessage } from "./helpers";
describe("isSameBytes", () => {
test("Should return true for same references", () => {
const arr = new Uint8Array([0, 1, 2, 3, 4, 5]);
expect(isSameBytes(arr, arr)).toBe(true);
});
test("Should return true for same bytes", () => {
const foo = new Uint8Array([0, 1, 2, 3, 4, 5]);
const bar = new Uint8Array([0, 1, 2, 3, 4, 5]);
expect(isSameBytes(foo, bar)).toBe(true);
});
test("Should return false for different bytes", () => {
const foo = new Uint8Array([0, 1, 2, 3, 4, 5]);
const bar = new Uint8Array([0, 1, 2, 0, 4, 5]);
expect(isSameBytes(foo, bar)).toBe(false);
});
test("Should return false for different length bytes", () => {
const foo = new Uint8Array([0, 1, 2]);
const bar = new Uint8Array([0, 1, 2, 3, 4, 5]);
expect(isSameBytes(foo, bar)).toBe(false);
});
});
describe("maskMessage", () => {
test("Should return empty message if mask is empty", () => {
expect(
maskMessage(DateSchema, { fields: [] }, {
year: 2000,
month: 1,
day: 1,
} as MessageInitShape<typeof DateSchema>),
).toEqual({});
});
test("Should mask field", () => {
expect(
maskMessage(DateSchema, { fields: [DateSchema.field.month.number] }, {
year: 2000,
month: 1,
day: 1,
} as MessageInitShape<typeof DateSchema>),
).toEqual({
month: 1,
});
});
test("Should return every field on full mask", () => {
expect(
maskMessage(
DateSchema,
{
fields: [
DateSchema.field.year.number,
DateSchema.field.month.number,
DateSchema.field.day.number,
],
},
{
year: 2000,
month: 1,
day: 1,
} as MessageInitShape<typeof DateSchema>,
),
).toEqual({
year: 2000,
month: 1,
day: 1,
});
});
test("Should keep $typeName", () => {
const masked = maskMessage(
DateSchema,
{ fields: [DateSchema.field.day.number] },
create(DateSchema, {
year: 2000,
month: 1,
day: 1,
}),
);
expect(masked.$typeName).not.toBeEmpty();
expect(masked.day).toBe(1);
expect(masked.year).toBeEmpty();
expect(masked.month).toBeEmpty();
});
test("Should not mutate input message", () => {
const input = create(DateSchema, {
year: 2000,
month: 1,
day: 1,
});
maskMessage(DateSchema, { fields: [DateSchema.field.day.number] }, input);
expect(input.year).toBe(2000);
expect(input.month).toBe(1);
expect(input.day).toBe(1);
});
});