@intentius/behold 0.29.42 → 0.29.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/web/app.js +12 -8
- package/web/dots-cards.js +362 -0
- package/web/dots-cards.test.js +303 -0
- package/web/dots.js +353 -64
- package/web/embed.js +16 -0
- package/web/embed.test.js +19 -1
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
GRID,
|
|
4
|
+
CARD_TEXT,
|
|
5
|
+
bestFit,
|
|
6
|
+
innerOf,
|
|
7
|
+
membersOf,
|
|
8
|
+
gridScale,
|
|
9
|
+
gridSlots,
|
|
10
|
+
cardSize,
|
|
11
|
+
shortType,
|
|
12
|
+
cardLabel,
|
|
13
|
+
cardNeed,
|
|
14
|
+
rosterLod,
|
|
15
|
+
fitFront,
|
|
16
|
+
wrapType,
|
|
17
|
+
cardShape,
|
|
18
|
+
cardText,
|
|
19
|
+
cardTextAlpha,
|
|
20
|
+
pickMark,
|
|
21
|
+
cardsMark,
|
|
22
|
+
cardsEstate,
|
|
23
|
+
middleCard,
|
|
24
|
+
cardsFrame,
|
|
25
|
+
minViewWidth,
|
|
26
|
+
tipSpot,
|
|
27
|
+
} from "./dots-cards.js";
|
|
28
|
+
import { LOD, cardMorph } from "./dots-lod.js";
|
|
29
|
+
import { BOX } from "./dots-scene.js";
|
|
30
|
+
|
|
31
|
+
// A face about as wide as the UI's: 0.55 em a character.
|
|
32
|
+
const measure = (s, px = CARD_TEXT.name) => s.length * px * 0.55;
|
|
33
|
+
|
|
34
|
+
// A layout as web/dots-scene.js's layoutAt makes one: slots, boxes and owners.
|
|
35
|
+
function layout(estates) {
|
|
36
|
+
const slots = new Map();
|
|
37
|
+
const boxes = new Map();
|
|
38
|
+
const owners = new Map();
|
|
39
|
+
for (const [e, { box, addrs }] of Object.entries(estates)) {
|
|
40
|
+
boxes.set(e, box);
|
|
41
|
+
for (const a of addrs) {
|
|
42
|
+
slots.set(a, { x: box.x + 2, y: box.y + 5 });
|
|
43
|
+
owners.set(a, e);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { slots, boxes, owners };
|
|
47
|
+
}
|
|
48
|
+
const box = (x, y, w, h) => ({ x, y, w, h, members: 1 });
|
|
49
|
+
const ids = (pre, n) => Array.from({ length: n }, (_, i) => `${pre}.r${String(i).padStart(2, "0")}`);
|
|
50
|
+
|
|
51
|
+
describe("the card grid fills each estate's box", () => {
|
|
52
|
+
it("finds the largest cell that fits n cells in rows", () => {
|
|
53
|
+
// 4 cells 2 wide in a 4 x 2 box: two rows of two, each 1 high.
|
|
54
|
+
expect(bestFit(4, 2, 4, 2)).toBe(1);
|
|
55
|
+
expect(bestFit(10, 1, 1, 2)).toBe(1);
|
|
56
|
+
expect(bestFit(0, 5, 3)).toBe(0);
|
|
57
|
+
expect(bestFit(5, 5, 0)).toBe(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("puts every card inside its box's inner rect, none overlapping, in unit order", () => {
|
|
61
|
+
const L = layout({ a: { box: box(0, 0, 30, 20), addrs: ids("a", 9) }, b: { box: box(40, 0, 12, 14), addrs: ids("b", 3) } });
|
|
62
|
+
const k = gridScale([L], (x) => x);
|
|
63
|
+
const cells = gridSlots(L, k, (x) => x);
|
|
64
|
+
const c = cardSize(k);
|
|
65
|
+
expect(cells.size).toBe(12);
|
|
66
|
+
for (const [e] of L.boxes) {
|
|
67
|
+
const r = innerOf(L.boxes.get(e));
|
|
68
|
+
const mine = [...cells].filter(([a]) => L.owners.get(a) === e).map(([, p]) => p);
|
|
69
|
+
for (const p of mine) {
|
|
70
|
+
expect(p.x - c.w / 2).toBeGreaterThanOrEqual(r.x - 1e-9);
|
|
71
|
+
expect(p.x + c.w / 2).toBeLessThanOrEqual(r.x + r.w + 1e-9);
|
|
72
|
+
expect(p.y - c.h / 2).toBeGreaterThanOrEqual(r.y - 1e-9);
|
|
73
|
+
expect(p.y + c.h / 2).toBeLessThanOrEqual(r.y + r.h + 1e-9);
|
|
74
|
+
}
|
|
75
|
+
for (let i = 0; i < mine.length; i++)
|
|
76
|
+
for (let j = i + 1; j < mine.length; j++) expect(Math.abs(mine[i].x - mine[j].x) >= c.w || Math.abs(mine[i].y - mine[j].y) >= c.h).toBe(true);
|
|
77
|
+
}
|
|
78
|
+
// Reading order is unit order: the first card is top-left.
|
|
79
|
+
const first = cells.get("a.r00");
|
|
80
|
+
for (const a of ids("a", 9)) expect(cells.get(a).y >= first.y && (cells.get(a).y > first.y || cells.get(a).x >= first.x)).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("takes one cell size for every estate and moment: the tightest box's, capped", () => {
|
|
84
|
+
const roomy = layout({ a: { box: box(0, 0, 200, 200), addrs: ids("a", 1) } });
|
|
85
|
+
expect(gridScale([roomy], (x) => x)).toBe(GRID.maxK);
|
|
86
|
+
const tight = layout({ a: { box: box(0, 0, 200, 200), addrs: ids("a", 1) }, b: { box: box(0, 0, 3 + 2 * BOX.pad, 3 + 2 * BOX.pad + BOX.title), addrs: ids("b", 1) } });
|
|
87
|
+
expect(gridScale([roomy, tight], (x) => x)).toBeCloseTo(3 / GRID.aspect, 9);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("gives a card several times the room of a dot's slot", () => {
|
|
91
|
+
// A one-unit box: an anchor and its ring, as web/dots-layout.js packs it (a 9-pitch cell holds about 6).
|
|
92
|
+
const L = layout({ a: { box: box(0, 0, 9 + 2 * BOX.pad, 9 + 2 * BOX.pad + BOX.title), addrs: ids("a", 6) } });
|
|
93
|
+
const c = cardSize(gridScale([L], (x) => x));
|
|
94
|
+
expect(c.w).toBeGreaterThan(3 * 0.88);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("groups an estate's members by the key it is given", () => {
|
|
98
|
+
const L = layout({ a: { box: box(0, 0, 30, 20), addrs: ["z", "y", "x"] } });
|
|
99
|
+
expect(membersOf(L, (a) => ({ z: "1", y: "2", x: "3" })[a]).get("a")).toEqual(["z", "y", "x"]);
|
|
100
|
+
expect(membersOf(L).get("a")).toEqual(["x", "y", "z"]);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("roster cards: words from a roster row", () => {
|
|
105
|
+
it("labels a card with the resource's name and index, and its type without the provider's prefix", () => {
|
|
106
|
+
expect(cardLabel({ address: 'module.team_a.aws_s3_bucket.logs["eu"]', type: "aws_s3_bucket" })).toEqual({ name: 'logs["eu"]', type: "s3_bucket", address: 'module.team_a.aws_s3_bucket.logs["eu"]' });
|
|
107
|
+
expect(cardLabel({ address: "aws_iam_role.svc_role[2]", type: "" })).toMatchObject({ name: "svc_role[2]", type: "iam_role" });
|
|
108
|
+
expect(cardLabel({ address: "data.aws_vpc.main", type: "aws_vpc" })).toMatchObject({ name: "main", type: "vpc" });
|
|
109
|
+
});
|
|
110
|
+
it("keeps a type that is all prefix, and terraform's own", () => {
|
|
111
|
+
expect(shortType("aws_iam_role_policy_attachment")).toBe("iam_role_policy_attachment");
|
|
112
|
+
expect(shortType("terraform_data")).toBe("terraform_data");
|
|
113
|
+
expect(shortType("null")).toBe("null");
|
|
114
|
+
expect(shortType("")).toBe("");
|
|
115
|
+
});
|
|
116
|
+
it("never shows behold's own suffix for two rows with one address", () => {
|
|
117
|
+
expect(cardLabel({ address: "aws_sqs_queue.jobs#2", type: "aws_sqs_queue" })).toMatchObject({ name: "jobs", address: "aws_sqs_queue.jobs" });
|
|
118
|
+
});
|
|
119
|
+
it("falls back to the whole address when it names no resource", () => {
|
|
120
|
+
expect(cardLabel({ address: "odd", type: "" })).toMatchObject({ name: "odd", type: "" });
|
|
121
|
+
expect(cardLabel(null)).toMatchObject({ name: "", type: "" });
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("the card level starts where the names fit", () => {
|
|
126
|
+
const labels = [
|
|
127
|
+
{ name: "team_0001_managed_attach", type: "iam_role_policy_attachment" },
|
|
128
|
+
{ name: "team_0001_role", type: "iam_role" },
|
|
129
|
+
];
|
|
130
|
+
it("is wide enough for the longest name at the name's face and the longest type at the type's", () => {
|
|
131
|
+
const need = cardNeed(labels, measure);
|
|
132
|
+
const room = need - CARD_TEXT.pad * 2 - CARD_TEXT.bar;
|
|
133
|
+
expect(measure("team_0001_managed_attach", CARD_TEXT.name)).toBeLessThanOrEqual(room);
|
|
134
|
+
expect(measure("iam_role_policy_attachment", CARD_TEXT.type)).toBeLessThanOrEqual(room);
|
|
135
|
+
});
|
|
136
|
+
it("is clamped", () => {
|
|
137
|
+
expect(cardNeed([{ name: "a", type: "" }], measure)).toBe(CARD_TEXT.minW);
|
|
138
|
+
expect(cardNeed([{ name: "x".repeat(200), type: "" }], measure)).toBe(CARD_TEXT.maxW);
|
|
139
|
+
});
|
|
140
|
+
it("scales the card's level of detail to it", () => {
|
|
141
|
+
const lod = rosterLod(180);
|
|
142
|
+
expect(cardMorph(180, { lod })).toBe(0);
|
|
143
|
+
expect(cardMorph((180 * LOD.dotPx) / LOD.cardPx, { lod })).toBe(1);
|
|
144
|
+
});
|
|
145
|
+
it("shows a whole name and a whole type on a card at that width", () => {
|
|
146
|
+
const need = cardNeed(labels, measure);
|
|
147
|
+
const h = (need * cardSize(1).h) / cardSize(1).w;
|
|
148
|
+
const lines = cardText({ name: "team_0001_managed_attach", type: "iam_role_policy_attachment" }, { w: need, h, need }, measure);
|
|
149
|
+
expect(lines.map((l) => l.text)).toEqual(["team_0001_managed_attach", "iam_role_policy_attachment"]);
|
|
150
|
+
expect(lines[0].size).toBe(CARD_TEXT.name);
|
|
151
|
+
expect(lines[1].size).toBe(CARD_TEXT.type);
|
|
152
|
+
});
|
|
153
|
+
it("keeps what fits at that width fitting as the card grows", () => {
|
|
154
|
+
const need = cardNeed(labels, measure);
|
|
155
|
+
for (const k of [1.2, 1.5, 2]) {
|
|
156
|
+
const w = need * k;
|
|
157
|
+
const lines = cardText({ name: "team_0001_managed_attach", type: "iam_role" }, { w, h: w * 0.29, need }, measure);
|
|
158
|
+
expect(lines[0].text).toBe("team_0001_managed_attach");
|
|
159
|
+
for (const l of lines) expect(measure(l.text, l.size)).toBeLessThanOrEqual(w - CARD_TEXT.pad * 2 - CARD_TEXT.bar);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe("fitting the words", () => {
|
|
165
|
+
it("keeps a name that fits", () => {
|
|
166
|
+
expect(fitFront("logs", 100, measure)).toBe("logs");
|
|
167
|
+
});
|
|
168
|
+
it("cuts a name still too long from the front, keeping the longest tail that fits", () => {
|
|
169
|
+
const s = "team_platform_ingest_dead_letter_queue";
|
|
170
|
+
const room = 70;
|
|
171
|
+
const out = fitFront(s, room, measure);
|
|
172
|
+
expect(out.startsWith("…")).toBe(true);
|
|
173
|
+
expect(s.endsWith(out.slice(1))).toBe(true);
|
|
174
|
+
expect(measure(out)).toBeLessThanOrEqual(room);
|
|
175
|
+
expect(measure(`…${s.slice(s.length - out.length)}`)).toBeGreaterThan(room);
|
|
176
|
+
});
|
|
177
|
+
it("draws nothing with no room", () => {
|
|
178
|
+
expect(fitFront("logs", 0, measure)).toBe("");
|
|
179
|
+
expect(fitFront("logs", 3, measure)).toBe("");
|
|
180
|
+
expect(fitFront("", 100, measure)).toBe("");
|
|
181
|
+
});
|
|
182
|
+
it("wraps a type after an underscore, near the middle, when it does not fit one line", () => {
|
|
183
|
+
const m = (s) => measure(s, CARD_TEXT.type);
|
|
184
|
+
const two = wrapType("iam_role_policy_attachment", 100, m);
|
|
185
|
+
expect(two).toEqual(["iam_role_policy_", "attachment"]);
|
|
186
|
+
expect(wrapType("iam_role_policy_attachment", 10, m)).toBeNull();
|
|
187
|
+
expect(wrapType("nounderscore", 100, m)).toBeNull();
|
|
188
|
+
});
|
|
189
|
+
it("puts a too-wide type on two lines on a tall card, and cuts it on a short one", () => {
|
|
190
|
+
const label = { name: "role", type: "iam_role_policy_attachment" };
|
|
191
|
+
const tall = cardText(label, { w: 120, h: 60, need: 120 }, measure);
|
|
192
|
+
expect(tall.map((l) => l.text)).toEqual(["role", "iam_role_policy_", "attachment"]);
|
|
193
|
+
const short = cardText(label, { w: 120, h: 36, need: 120 }, measure);
|
|
194
|
+
expect(short.length).toBe(2);
|
|
195
|
+
expect(short[1].text.startsWith("…")).toBe(true);
|
|
196
|
+
});
|
|
197
|
+
it("leaves a line out rather than shrink it", () => {
|
|
198
|
+
const label = { name: 'logs["eu"]', type: "cloudwatch_log_group" };
|
|
199
|
+
expect(cardText(label, { w: 30, h: 60 }, measure)).toEqual([]);
|
|
200
|
+
expect(cardText(label, { w: 200, h: 10 }, measure)).toEqual([]);
|
|
201
|
+
expect(cardText(label, { w: 200, h: 20 }, measure).map((l) => l.ink)).toEqual(["fg"]);
|
|
202
|
+
expect(cardText({ name: "x", type: "" }, { w: 200, h: 80 }, measure).length).toBe(1);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe("the dot grows into its card", () => {
|
|
207
|
+
const sizes = { dotR: 4, cardW: 120, cardH: 57 };
|
|
208
|
+
it("is the dot at 0 and the card at 1, about the same centre", () => {
|
|
209
|
+
expect(cardShape(50, 40, 0, sizes)).toEqual({ x: 46, y: 36, w: 8, h: 8, r: 4 });
|
|
210
|
+
const c = cardShape(50, 40, 1, sizes);
|
|
211
|
+
expect(c.w).toBe(120);
|
|
212
|
+
expect(c.h).toBe(57);
|
|
213
|
+
expect(c.x + c.w / 2).toBe(50);
|
|
214
|
+
expect(c.y + c.h / 2).toBe(40);
|
|
215
|
+
});
|
|
216
|
+
it("grows monotonically in between", () => {
|
|
217
|
+
let prev = 0;
|
|
218
|
+
for (let u = 0; u <= 1; u += 0.05) {
|
|
219
|
+
const c = cardShape(0, 0, u, sizes);
|
|
220
|
+
expect(c.w).toBeGreaterThanOrEqual(prev);
|
|
221
|
+
prev = c.w;
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
it("fades the words in only near the end of the morph", () => {
|
|
225
|
+
expect(cardTextAlpha(0.6)).toBe(0);
|
|
226
|
+
expect(cardTextAlpha(0.8)).toBeGreaterThan(0);
|
|
227
|
+
expect(cardTextAlpha(1)).toBe(1);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe("pickMark: hover, tap and the arrow keys find the card under the pointer", () => {
|
|
232
|
+
const marks = { px: [10, 100, 300], py: [10, 100, 300], pvis: [1, 1, 1], pw: [0, 40, 0], ph: [0, 20, 0] };
|
|
233
|
+
it("picks a card anywhere on it, a dot only near its centre", () => {
|
|
234
|
+
expect(pickMark(135, 115, marks)).toBe(1);
|
|
235
|
+
expect(pickMark(12, 12, marks)).toBe(0);
|
|
236
|
+
expect(pickMark(30, 30, marks)).toBe(-1);
|
|
237
|
+
expect(pickMark(310, 300, marks, { within: 12 })).toBe(2);
|
|
238
|
+
});
|
|
239
|
+
it("skips a mark that is not drawn", () => {
|
|
240
|
+
expect(pickMark(100, 100, { ...marks, pvis: [1, 0, 1] })).toBe(-1);
|
|
241
|
+
});
|
|
242
|
+
it("prefers the card whose centre is nearer where two meet", () => {
|
|
243
|
+
const two = { px: [0, 60], py: [0, 0], pvis: [1, 1], pw: [40, 40], ph: [20, 20] };
|
|
244
|
+
expect(pickMark(35, 0, two)).toBe(1);
|
|
245
|
+
expect(pickMark(25, 0, two)).toBe(0);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe("the rail's Cards mark", () => {
|
|
250
|
+
it("counts the roster's cards when the canvas draws them", () => {
|
|
251
|
+
expect(cardsMark({ resources: 82, seeded: 0, svgCards: 4, own: true })).toEqual({ count: 82, words: "82 cards", enabled: true });
|
|
252
|
+
});
|
|
253
|
+
it("counts the SVG's resource cards when the SVG draws them", () => {
|
|
254
|
+
expect(cardsMark({ resources: 1041, seeded: 1000, svgCards: 1000, own: false }).words).toBe("1,000 cards");
|
|
255
|
+
});
|
|
256
|
+
it("counts the SVG's own cards (a live carve's roots) when none is a resource's and the stage is the SVG's", () => {
|
|
257
|
+
expect(cardsMark({ resources: 82, seeded: 0, svgCards: 4, own: false })).toEqual({ count: 4, words: "4 cards", enabled: true });
|
|
258
|
+
expect(cardsMark({ resources: 82, seeded: 0, svgCards: 1, own: false }).words).toBe("1 card");
|
|
259
|
+
});
|
|
260
|
+
it("says so, and cannot be pressed, when no card can draw", () => {
|
|
261
|
+
expect(cardsMark({ resources: 82, seeded: 0, svgCards: 0, own: false })).toEqual({ count: 0, words: "no cards", enabled: false });
|
|
262
|
+
});
|
|
263
|
+
it("lands on the director's estate when it holds cards, else the source, else the fullest", () => {
|
|
264
|
+
const counts = new Map([["src", 0], ["team-a", 12], ["team-b", 30]]);
|
|
265
|
+
expect(cardsEstate("team-a", "src", counts)).toBe("team-a");
|
|
266
|
+
expect(cardsEstate("gone", "src", counts)).toBe("team-b");
|
|
267
|
+
expect(cardsEstate(null, "team-a", counts)).toBe("team-a");
|
|
268
|
+
expect(cardsEstate(null, "src", new Map())).toBeNull();
|
|
269
|
+
});
|
|
270
|
+
it("centres on a card, never on the gap between cards", () => {
|
|
271
|
+
const pts = [{ x: 0, y: 0 }, { x: 10, y: 0 }, { x: 0, y: 10 }, { x: 10, y: 10 }, { x: 4, y: 6 }];
|
|
272
|
+
expect(middleCard(pts)).toEqual({ x: 4, y: 6 });
|
|
273
|
+
expect(middleCard([])).toBeNull();
|
|
274
|
+
expect(cardsFrame({ x: 120, y: 210 }, 60, 0.5)).toEqual([90, 195, 60, 30]);
|
|
275
|
+
});
|
|
276
|
+
it("lands where the cards are fully drawn, and the zoom in stops a little past it", () => {
|
|
277
|
+
const paneW = 1300;
|
|
278
|
+
const cardW = 40;
|
|
279
|
+
const need = 180;
|
|
280
|
+
// A card at the Cards mark (web/dots-lod.js railMarks, cardAt = need * 1.1) is fully drawn.
|
|
281
|
+
const markW = (paneW * cardW) / (need * 1.1);
|
|
282
|
+
expect(cardMorph((cardW * paneW) / markW, { lod: rosterLod(need) })).toBe(0);
|
|
283
|
+
// The narrowest view keeps the mark within reach, and stops not far past it.
|
|
284
|
+
const minW = minViewWidth(paneW, cardW, need);
|
|
285
|
+
expect(minW).toBeLessThan(markW);
|
|
286
|
+
expect((cardW * paneW) / minW).toBeCloseTo(need * 1.8, 6);
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe("a card's tip", () => {
|
|
291
|
+
const pane = { tipW: 300, tipH: 40, paneW: 1440, paneH: 900 };
|
|
292
|
+
it("sits right of and under the mark", () => {
|
|
293
|
+
expect(tipSpot(100, 100, pane)).toEqual({ left: 112, top: 114 });
|
|
294
|
+
});
|
|
295
|
+
it("keeps clear of the rail on the right", () => {
|
|
296
|
+
const s = tipSpot(1300, 100, { ...pane, rightInset: 160 });
|
|
297
|
+
expect(s.left + pane.tipW).toBeLessThanOrEqual(1440 - 160);
|
|
298
|
+
});
|
|
299
|
+
it("goes over the mark when the pane's foot leaves no room under it", () => {
|
|
300
|
+
const s = tipSpot(100, 880, { ...pane, above: 840 });
|
|
301
|
+
expect(s.top + pane.tipH).toBeLessThanOrEqual(840);
|
|
302
|
+
});
|
|
303
|
+
});
|