-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas-hud.test.ts
More file actions
257 lines (241 loc) · 7.05 KB
/
Copy pathcanvas-hud.test.ts
File metadata and controls
257 lines (241 loc) · 7.05 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
// @vitest-environment jsdom
import { describe, expect, it } from "vitest";
import * as canvasHud from "./canvas-hud";
type HudPage = "overview" | "navigation" | "news" | "todo";
type Rectangle = {
style: string;
args: [number, number, number, number];
};
type TextRecord = {
style: string;
font: string;
value: string;
};
type PathRecord = {
style: string;
width: number;
points: Array<[number, number]>;
};
function renderHud(page?: HudPage) {
const rectangles: Rectangle[] = [];
const texts: TextRecord[] = [];
const strokedPaths: PathRecord[] = [];
const filledPaths: PathRecord[] = [];
let fillStyle = "";
let strokeStyle = "";
let lineWidth = 1;
let currentPath: Array<[number, number]> = [];
const context = {
imageSmoothingEnabled: true,
font: "",
textAlign: "start",
textBaseline: "alphabetic",
lineCap: "butt",
lineJoin: "miter",
get fillStyle() {
return fillStyle;
},
set fillStyle(value: string | CanvasGradient | CanvasPattern) {
fillStyle = String(value);
},
get strokeStyle() {
return strokeStyle;
},
set strokeStyle(value: string | CanvasGradient | CanvasPattern) {
strokeStyle = String(value);
},
get lineWidth() {
return lineWidth;
},
set lineWidth(value: number) {
lineWidth = value;
},
fillRect: (x: number, y: number, width: number, height: number) => {
rectangles.push({ style: fillStyle, args: [x, y, width, height] });
},
fillText: (value: string) => {
texts.push({ style: fillStyle, font: context.font, value });
},
beginPath: () => {
currentPath = [];
},
moveTo: (x: number, y: number) => {
currentPath.push([x, y]);
},
lineTo: (x: number, y: number) => {
currentPath.push([x, y]);
},
closePath: () => undefined,
stroke: () => {
strokedPaths.push({
style: strokeStyle,
width: lineWidth,
points: [...currentPath],
});
},
fill: () => {
filledPaths.push({
style: fillStyle,
width: 0,
points: [...currentPath],
});
},
};
const canvas = {
width: 0,
height: 0,
getContext: () => context,
} as unknown as HTMLCanvasElement;
canvasHud.drawDenseCanvasHud(
canvas,
new Date(2026, 6, 26, 14, 37, 42),
page,
);
return {
canvas,
rectangles,
texts,
values: texts.map(({ value }) => value),
strokedPaths,
filledPaths,
};
}
describe("dense Canvas HUD", () => {
it("defaults to a non-navigation news overview", () => {
const hud = renderHud();
expect(canvasHud.HUD_PAGES).toEqual([
"overview",
"navigation",
"news",
"todo",
]);
expect(hud.values).toEqual(expect.arrayContaining([
"14:37:42",
"HONGDAE 23°C 맑음",
"NE 047°",
"01 / 04",
"NEWS // LOCAL 01",
"2호선 정상 운행",
"홍대입구역 혼잡도",
"보통",
"BRIEF // 02",
"오늘 23°C · 맑음",
"강수 10%",
"-24 dBFS",
"TODO // ACTIVE",
]));
expect(hud.values).not.toContain("NAV // ROUTE 01");
expect(hud.values).not.toContain("다음 교차로");
expect(hud.values).not.toContain("우회전");
});
it("wraps page navigation in both directions", () => {
const getAdjacentHudPage = (
canvasHud as unknown as {
getAdjacentHudPage?: (
page: HudPage,
direction: "next" | "previous",
) => HudPage;
}
).getAdjacentHudPage;
expect(getAdjacentHudPage).toBeTypeOf("function");
if (!getAdjacentHudPage) return;
expect(getAdjacentHudPage("overview", "next")).toBe("navigation");
expect(getAdjacentHudPage("todo", "next")).toBe("overview");
expect(getAdjacentHudPage("overview", "previous")).toBe("todo");
});
it("keeps the approved navigation dashboard on its own page", () => {
const hud = renderHud("navigation");
expect(hud.values).toEqual(expect.arrayContaining([
"02 / 04",
"MAP // LOCAL 120m",
"NAV // ROUTE 01",
"120m",
"다음 교차로",
"우회전",
"TODO // ACTIVE",
]));
expect(hud.strokedPaths).toEqual(expect.arrayContaining([
expect.objectContaining({ style: "#aaaaaa", width: 6 }),
expect.objectContaining({ style: "#ffffff", width: 2 }),
expect.objectContaining({ style: "#aaaaaa", width: 8 }),
expect.objectContaining({ style: "#ffffff", width: 3 }),
]));
});
it("gives news a large focused page", () => {
expect(renderHud("news").values).toEqual(expect.arrayContaining([
"03 / 04",
"NEWS // FOCUS",
"2호선 정상 운행",
"홍대입구역 혼잡도 보통",
"오늘 23°C · 맑음",
"강수 확률 10%",
]));
});
it("gives tasks and device status a large focused page", () => {
const hud = renderHud("todo");
expect(hud.values).toEqual(expect.arrayContaining([
"04 / 04",
"TODO // FOCUS",
"지하철역으로 이동",
"우산 챙기기",
"경로 확인",
"AUDIO // STATUS",
"LINK // G2 + R1",
]));
expect(hud.strokedPaths).toEqual(expect.arrayContaining([
expect.objectContaining({ style: "#ffffff", width: 2 }),
]));
});
it("preserves display size, palette, map layering, and Canvas checkboxes", () => {
const hud = renderHud();
const paintedStyles = [
...hud.rectangles.map(({ style }) => style),
...hud.texts.map(({ style }) => style),
...hud.strokedPaths.map(({ style }) => style),
...hud.filledPaths.map(({ style }) => style),
];
expect(hud.canvas.width).toBe(576);
expect(hud.canvas.height).toBe(288);
expect(new Set(paintedStyles)).toEqual(new Set([
"#000000",
"#ffffff",
"#aaaaaa",
"#555555",
]));
expect(hud.rectangles).toEqual(expect.arrayContaining([
{ style: "#000000", args: [0, 0, 576, 288] },
{ style: "#555555", args: [8, 72, 16, 1] },
{ style: "#555555", args: [204, 72, 16, 1] },
{ style: "#555555", args: [404, 72, 16, 1] },
{ style: "#555555", args: [404, 142, 16, 1] },
{ style: "#555555", args: [552, 279, 16, 1] },
{ style: "#ffffff", args: [416, 190, 12, 12] },
{ style: "#000000", args: [419, 193, 6, 6] },
{ style: "#aaaaaa", args: [416, 255, 10, 10] },
{ style: "#000000", args: [419, 258, 4, 4] },
]));
expect(hud.strokedPaths).toEqual(expect.arrayContaining([
expect.objectContaining({ style: "#aaaaaa", width: 6 }),
expect.objectContaining({ style: "#ffffff", width: 2 }),
expect.objectContaining({
style: "#ffffff",
width: 2,
points: [[418, 260], [420, 263], [425, 257]],
}),
]));
expect(hud.filledPaths.length).toBeGreaterThanOrEqual(1);
expect(hud.texts.find(({ value }) => value === "14:37:42")?.font)
.toContain("22px");
for (const forbiddenValue of [
"14:37",
"MISSION ACTIVE",
"ROUTE UPDATED",
"ACC",
"X +0.12",
"Y -0.03",
"Z +0.98",
]) {
expect(hud.values).not.toContain(forbiddenValue);
}
});
});