-
Notifications
You must be signed in to change notification settings - Fork 1
/
adfice-util.test.js
170 lines (146 loc) · 4.32 KB
/
adfice-util.test.js
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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2021-2024 Stichting Open Electronics Lab
// vim: set sts=4 shiftwidth=4 expandtab :
"use strict";
const autil = require('./adfice-util')
const fs = require('fs');
test('assert true', () => {
autil.assert(true);
})
test('assert false', () => {
let caught = null;
try {
autil.assert(1 == 2);
} catch (error) {
caught = error;
}
expect(caught !== null).toBe(true);
let msg = "" + caught;
expect(msg).toContain("Assertion failed");
})
test('assert(false, messge)', () => {
let caught = null;
try {
autil.assert(1 == 2, "Foo bar");
} catch (error) {
caught = error;
}
expect(caught !== null).toBe(true);
let msg = "" + caught;
expect(msg).toContain("Foo bar");
})
test('split freetext advice strings', () => {
let example = "initial:{{free text: pre-filled: first free text}}" +
" some addtional text:{{free text}}";
let expected = [{
id: 0,
text: "initial:",
editable: false
}, {
id: 1,
text: "first free text",
editable: true
}, {
id: 2,
text: "some addtional text:",
editable: false
}, {
id: 3,
text: "",
editable: true
}];
expect(autil.split_freetext(example)).toStrictEqual(expected);
expected = [{
id: 0,
text: "foo",
editable: false
}];
expect(autil.split_freetext("foo")).toStrictEqual(expected);
expect(autil.split_freetext(null)).toStrictEqual([]);
})
test('split freetext handle bad strings', () => {
let example = "initial:{{free text: pre-filled: first free text}}" +
" some addtional text:{{free text}}";
expect(autil.split_freetext("{free text}}")).toStrictEqual([{
id: 0,
text: "{free text",
editable: false
}, {
id: 1,
text: "BAD DATA",
editable: true
}]);
expect(autil.split_freetext("{free text:}}")).toStrictEqual([{
id: 0,
text: "{free text:",
editable: false
}, {
id: 1,
text: "BAD DATA",
editable: true
}]);
expect(autil.split_freetext("{{free text:}")).toStrictEqual([{
id: 1,
text: "",
editable: true
}]);
});
test('json serialization file round-trip', async function() {
let path = 'adfice-util.test.garbage.json';
try {
fs.unlinkSync(path);
} catch (ignore) {
// it is okay that the file does not exist
}
let obj1 = {
foo: 'bar',
whiz: 'bang',
};
await autil.to_json_file(path, obj1);
let obj2 = await autil.from_json_file(path);
expect(obj2).toStrictEqual(obj1);
fs.unlinkSync(path);
});
function expect_uuid4_version_and_variant(bytes) {
// version 4 UUIDs: xxxxxxxx-xxxx-4xxx-Vxxx-xxxxxxxxxxxx
// the 2 most significant bits of V should be 0b10
expect(bytes[6] & 0xF0).toBe(0x40);
expect(bytes[8] & 0xC0).toBe(0x80);
}
test('uuid4_new', function() {
let uuid_bytes = autil.uuid4_new();
expect(uuid_bytes.length).toBe(16);
expect_uuid4_version_and_variant(uuid_bytes);
});
const uuid4_regex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89ABab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
test('uuid4_new_string', function() {
let uuid_str_1 = autil.uuid4_new_string();
expect(uuid_str_1.length).toBe(36);
expect(uuid_str_1).toMatch(uuid4_regex);
let uuid_str_2 = autil.uuid4_new_string();
expect(uuid_str_2.length).toBe(36);
expect(uuid_str_2).toMatch(uuid4_regex);
expect(uuid_str_2).not.toBe(uuid_str_1);
});
test('set_version_and_variant, uuid_bytes_to_string', function() {
let bytes = new Uint8Array(16);
bytes[15] = 3;
autil.uuid4_set_version_and_variant(bytes);
expect_uuid4_version_and_variant(bytes);
let uuid_str = autil.uuid_bytes_to_string(bytes);
expect(uuid_str).toBe("00000000-0000-4000-8000-000000000003");
});
test('tmp_path defaults', function() {
let prefix;
let suffix;
let path = autil.tmp_path(prefix, suffix);
expect(path).toMatch(/tmp\./);
});
test('string_to_hash', function(){
let string = "foo";
let hash = autil.string_to_hash(string);
expect(hash).toBe("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33");
string = "bar";
let hash2 = autil.string_to_hash(string);
expect(hash2).not.toBe(hash);
});