@jskit-ai/shell-web 0.2.55 → 0.2.57

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.
@@ -3,7 +3,7 @@ import { readFile } from "node:fs/promises";
3
3
  import test from "node:test";
4
4
  import { compileScript, compileTemplate, parse } from "@vue/compiler-sfc";
5
5
  import { computed, ref } from "vue";
6
- import { createShellPageActionsRegistry } from "../src/client/support/pageActions.js";
6
+ import { createShellPageActionsRegistry, resolveFoldedPageActions } from "../src/client/support/pageActions.js";
7
7
 
8
8
  function action(label, { primary = false, overflow = false } = {}) {
9
9
  return { id: label, state: computed(() => ({ label, primary, overflow })), attrs: {}, activate() {}, anchor: null };
@@ -52,6 +52,48 @@ test("overflow actions go to the menu at every width, in bar order, and an overf
52
52
  assert.deepEqual(menuLabels(registry), ["Edit", "Duplicate", "Delete", "Publish"]);
53
53
  });
54
54
 
55
+ test("a wider bar folds its last actions that don't fit into the menu, in bar order, and never the primary one", () => {
56
+ const isCompact = ref(false);
57
+ const registry = createShellPageActionsRegistry({ isCompact });
58
+ const [edit, share, duplicate, save, archive] = [
59
+ action("Edit"),
60
+ action("Share"),
61
+ action("Duplicate", { overflow: true }),
62
+ action("Save", { primary: true }),
63
+ action("Archive")
64
+ ];
65
+ [edit, share, duplicate, save, archive].forEach((entry) => registry.register(entry));
66
+ assert.deepEqual(registry.foldable.value.map((entry) => entry.state.value.label), ["Edit", "Share", "Archive"]);
67
+
68
+ registry.fold(1);
69
+ assert.deepEqual(menuLabels(registry), ["Duplicate", "Archive"]);
70
+ assert.equal(registry.inMenu(archive), true);
71
+ assert.equal(registry.inMenu(share), false);
72
+ registry.fold(2);
73
+ assert.deepEqual(menuLabels(registry), ["Share", "Duplicate", "Archive"], "the menu keeps bar order");
74
+ registry.fold(5);
75
+ assert.deepEqual(menuLabels(registry), ["Edit", "Share", "Duplicate", "Archive"], "the primary action stays in the bar");
76
+ assert.equal(registry.inMenu(save), false);
77
+
78
+ registry.fold(0);
79
+ isCompact.value = true;
80
+ assert.deepEqual(menuLabels(registry), ["Edit", "Share", "Duplicate", "Archive"], "a compact bar keeps its own rule");
81
+ });
82
+
83
+ test("a wider bar folds as few of its last actions as leave the rest and the overflow button beside the whole title", () => {
84
+ // 400px beside the title for four 100px actions; the overflow button is 48px.
85
+ const bar = { room: 400, fixed: 0, actions: [100, 100, 100, 100], menuButton: 48, menuShown: false };
86
+ assert.equal(resolveFoldedPageActions(bar), 0, "four actions that fit to the pixel, with no menu");
87
+ assert.equal(resolveFoldedPageActions({ ...bar, room: 399 }), 1, "one pixel short: the last action and the button");
88
+ assert.equal(resolveFoldedPageActions({ ...bar, room: 348 }), 1, "three actions and the button, to the pixel");
89
+ assert.equal(resolveFoldedPageActions({ ...bar, room: 347 }), 2);
90
+ assert.equal(resolveFoldedPageActions({ ...bar, menuShown: true }), 1, "the menu's own actions need the button too");
91
+ assert.equal(resolveFoldedPageActions({ ...bar, room: 400, fixed: 120 }), 2, "the primary action takes its width first");
92
+ assert.equal(resolveFoldedPageActions({ ...bar, room: 20 }), 4, "too little room: every one folds");
93
+ assert.equal(resolveFoldedPageActions({ ...bar, room: -50 }), 4, "a title wider than the bar");
94
+ assert.equal(resolveFoldedPageActions({ ...bar, actions: [] }), 0);
95
+ });
96
+
55
97
  test("ShellPageAction forwards its attributes to the button in each form and registers with the bar", async () => {
56
98
  const filename = new URL("../src/client/components/ShellPageAction.vue", import.meta.url);
57
99
  const source = await readFile(filename, "utf8");
@@ -26,6 +26,7 @@ test("an application without the provider places nothing", () => {
26
26
  assert.deepEqual(result.runtime.getPlacements(), []);
27
27
  assert.deepEqual(result.placement.context.value, {});
28
28
  assert.deepEqual(result.placement.mergeContext({ a: 1 }), {});
29
+ assert.equal(result.runtime.isContextPending(), false);
29
30
  app.unmount();
30
31
  });
31
32
 
@@ -41,6 +42,17 @@ test("the placement context follows the runtime while mounted", () => {
41
42
  result.replaceContext({ only: true }, "test");
42
43
  assert.deepEqual(runtime.getContext(), { only: true });
43
44
 
45
+ // A source says its part is still loading, and ends the wait by setting it again.
46
+ result.mergeContext({ permissions: [] }, "session", { pending: true });
47
+ assert.equal(runtime.isContextPending(), true);
48
+ result.mergeContext({ permissions: ["builds.read"] }, "session");
49
+ assert.equal(runtime.isContextPending(), false);
50
+ result.replaceContext({ permissions: [] }, "session", { pending: true });
51
+ assert.equal(runtime.isContextPending(), true);
52
+ assert.deepEqual(result.context.value, { permissions: [] });
53
+ result.replaceContext({ permissions: [] }, "session");
54
+ assert.equal(runtime.isContextPending(), false);
55
+
44
56
  app.unmount();
45
57
  runtime.setContext({ after: true });
46
58
  assert.equal(result.context.value.after, undefined);
@@ -330,6 +330,35 @@ test("web placement runtime notifies subscribers on placement and context update
330
330
  assert.deepEqual(events, ["placements.replaced", "context.updated"]);
331
331
  });
332
332
 
333
+ test("web placement runtime keeps the context pending until each source that said it is loading sets it again", () => {
334
+ const runtime = createWebPlacementRuntime({ components: createComponentsStub(), logger: silentLogger });
335
+ const events = [];
336
+ runtime.subscribe((event) => {
337
+ events.push(`${event.type} ${event.source}`);
338
+ });
339
+ assert.equal(runtime.isContextPending(), false);
340
+
341
+ runtime.setContext({ permissions: [] }, { source: "session", pending: true });
342
+ runtime.setContext({ plan: null }, { source: "billing", pending: true });
343
+ assert.equal(runtime.isContextPending(), true);
344
+
345
+ // Another source's context leaves the wait alone, and so does one of the two that end it.
346
+ runtime.setContext({ surface: "app" }, { source: "surface" });
347
+ runtime.setContext({ plan: "free" }, { source: "billing" });
348
+ assert.equal(runtime.isContextPending(), true);
349
+
350
+ runtime.setContext({ permissions: ["builds.read"] }, { source: "session" });
351
+ assert.equal(runtime.isContextPending(), false);
352
+ assert.deepEqual(runtime.getContext(), { permissions: ["builds.read"], plan: "free", surface: "app" });
353
+ assert.deepEqual(events, [
354
+ "context.updated session",
355
+ "context.updated billing",
356
+ "context.updated surface",
357
+ "context.updated billing",
358
+ "context.updated session"
359
+ ]);
360
+ });
361
+
333
362
  test("web placement runtime rejects duplicate placement ids", () => {
334
363
  const components = createComponentsStub();
335
364
  const runtime = createWebPlacementRuntime({ components, logger: silentLogger });
@@ -16,12 +16,29 @@ function createDrawerElement() {
16
16
  };
17
17
  }
18
18
 
19
- function mountDrawer(t, { compact = false, props = {} } = {}) {
19
+ function mountDrawer(t, { compact = false, drawn = true, fontsReady = Promise.resolve(), props = {} } = {}) {
20
20
  const frames = [];
21
21
  const drawerElement = createDrawerElement();
22
22
  const fontListeners = new Set();
23
+ // The drawer's element is on the page while the shell draws it.
24
+ const hasDrawer = ref(drawn);
25
+ // The page's live timers, each run by the test's clock (mocked where the test mocks it).
26
+ const timers = new Map();
27
+ let lastTimer = 0;
23
28
  const page = installPage(t, {
24
29
  window: {
30
+ setTimeout(callback, delay) {
31
+ const id = ++lastTimer;
32
+ timers.set(id, globalThis.setTimeout(() => {
33
+ timers.delete(id);
34
+ callback();
35
+ }, delay));
36
+ return id;
37
+ },
38
+ clearTimeout(id) {
39
+ globalThis.clearTimeout(timers.get(id));
40
+ timers.delete(id);
41
+ },
25
42
  requestAnimationFrame(callback) {
26
43
  frames.push(callback);
27
44
  return frames.length;
@@ -33,11 +50,11 @@ function mountDrawer(t, { compact = false, props = {} } = {}) {
33
50
  },
34
51
  document: {
35
52
  fonts: {
36
- ready: Promise.resolve(),
53
+ ready: fontsReady,
37
54
  addEventListener: (_type, listener) => fontListeners.add(listener),
38
55
  removeEventListener: (_type, listener) => fontListeners.delete(listener)
39
56
  },
40
- getElementById: () => drawerElement,
57
+ getElementById: () => (hasDrawer.value ? drawerElement : null),
41
58
  createRange: () => ({
42
59
  selectNodeContents() {},
43
60
  getBoundingClientRect: () => ({ left: 16, right: 16 + LABEL_TEXT_WIDTH, width: LABEL_TEXT_WIDTH })
@@ -77,6 +94,7 @@ function mountDrawer(t, { compact = false, props = {} } = {}) {
77
94
  navigationItemSpacing: 12,
78
95
  ...props
79
96
  },
97
+ hasDrawer,
80
98
  modalLayout: isCompactLayout,
81
99
  viewportWidth,
82
100
  navigationToggle,
@@ -89,9 +107,25 @@ function mountDrawer(t, { compact = false, props = {} } = {}) {
89
107
  }
90
108
  }
91
109
  }));
92
- return { app, drawerOpen, focused, fontListeners, frames, isCompactLayout, observers, page, result, viewportWidth };
110
+ return {
111
+ app,
112
+ drawerOpen,
113
+ focused,
114
+ fontListeners,
115
+ frames,
116
+ hasDrawer,
117
+ isCompactLayout,
118
+ observers,
119
+ page,
120
+ result,
121
+ timers,
122
+ viewportWidth
123
+ };
93
124
  }
94
125
 
126
+ // Lets every promise reaction run; a mocked clock leaves setImmediate alone.
127
+ const settlePromises = () => new Promise((resolve) => setImmediate(resolve));
128
+
95
129
  async function runFrames(frames) {
96
130
  await nextTick();
97
131
  await nextTick();
@@ -205,6 +239,100 @@ test("a content drawer's width follows its measured labels", async (t) => {
205
239
  app.unmount();
206
240
  });
207
241
 
242
+ test("a drawer that arrives after the shell mounted is watched and fitted to its labels", async (t) => {
243
+ const { app, frames, hasDrawer, observers, result } = mountDrawer(t, { drawn: false });
244
+ await runFrames(frames);
245
+ assert.equal(observers.length, 0);
246
+ assert.equal(result.resolvedDrawerWidth.value, 248);
247
+
248
+ hasDrawer.value = true;
249
+ await runFrames(frames);
250
+
251
+ assert.equal(observers.length, 1);
252
+ assert.equal(observers[0].connected, true);
253
+ assert.equal(result.resolvedDrawerWidth.value, 16 + LABEL_TEXT_WIDTH + 12 + 1);
254
+ app.unmount();
255
+ });
256
+
257
+ test("settling the drawer fits it at once, in place of the frame its measurement waited for", async (t) => {
258
+ const { app, frames, observers, result } = mountDrawer(t, { props: { drawerWidth: "content" } });
259
+ await nextTick();
260
+ await nextTick();
261
+ assert.equal(frames.filter(Boolean).length, 1);
262
+ assert.equal(result.resolvedDrawerWidth.value, 248);
263
+
264
+ await result.settleDrawerWidth();
265
+
266
+ assert.equal(result.resolvedDrawerWidth.value, 16 + LABEL_TEXT_WIDTH + 12 + 1);
267
+ assert.deepEqual(frames.filter(Boolean), []);
268
+ assert.equal(observers.at(-1).connected, true);
269
+ app.unmount();
270
+ });
271
+
272
+ test("settling the drawer waits for the fonts the page is loading, then fits it and releases its wait's timer", async (t) => {
273
+ t.mock.timers.enable({ apis: ["setTimeout"] });
274
+ let fontsArrive = () => {};
275
+ const fontsReady = new Promise((resolve) => {
276
+ fontsArrive = resolve;
277
+ });
278
+ const { app, result, timers } = mountDrawer(t, { fontsReady, props: { drawerWidth: "content" } });
279
+
280
+ const settled = result.settleDrawerWidth();
281
+ t.mock.timers.tick(30);
282
+ await settlePromises();
283
+ assert.equal(result.resolvedDrawerWidth.value, 248);
284
+
285
+ fontsArrive();
286
+ await settled;
287
+ assert.equal(result.resolvedDrawerWidth.value, 16 + LABEL_TEXT_WIDTH + 12 + 1);
288
+ assert.equal(timers.size, 0);
289
+ app.unmount();
290
+ });
291
+
292
+ test("settling the drawer waits at most 100 ms for a slow font, then fits it in the fallback font", async (t) => {
293
+ t.mock.timers.enable({ apis: ["setTimeout"] });
294
+ const { app, result, timers } = mountDrawer(t, { fontsReady: new Promise(() => {}), props: { drawerWidth: "content" } });
295
+
296
+ void result.settleDrawerWidth();
297
+ await settlePromises();
298
+ t.mock.timers.tick(99);
299
+ await settlePromises();
300
+ assert.equal(result.resolvedDrawerWidth.value, 248);
301
+
302
+ t.mock.timers.tick(1);
303
+ await settlePromises();
304
+ assert.equal(result.resolvedDrawerWidth.value, 16 + LABEL_TEXT_WIDTH + 12 + 1);
305
+ assert.equal(timers.size, 0);
306
+ app.unmount();
307
+ });
308
+
309
+ test("a shell unmounted while its drawer waits for a font releases the wait's timer and measures nothing", async (t) => {
310
+ t.mock.timers.enable({ apis: ["setTimeout"] });
311
+ const { app, result, timers } = mountDrawer(t, { fontsReady: new Promise(() => {}), props: { drawerWidth: "content" } });
312
+
313
+ void result.settleDrawerWidth();
314
+ await settlePromises();
315
+ assert.equal(timers.size, 1);
316
+
317
+ app.unmount();
318
+ assert.equal(timers.size, 0);
319
+ t.mock.timers.tick(100);
320
+ await settlePromises();
321
+ assert.equal(result.resolvedDrawerWidth.value, 248);
322
+ });
323
+
324
+ test("settling a drawer of fixed width measures nothing", async (t) => {
325
+ const { app, frames, result } = mountDrawer(t, { props: { drawerWidth: 300 } });
326
+ await nextTick();
327
+ await nextTick();
328
+
329
+ await result.settleDrawerWidth();
330
+
331
+ assert.equal(result.resolvedDrawerWidth.value, 300);
332
+ assert.deepEqual(frames.filter(Boolean), []);
333
+ app.unmount();
334
+ });
335
+
208
336
  test("a fixed drawer width is never measured", async (t) => {
209
337
  const { app, frames, result } = mountDrawer(t, { props: { drawerWidth: 300 } });
210
338
 
@@ -0,0 +1,75 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { watch } from "vue";
4
+ import { silentLogger } from "@jskit-ai/kernel/shared/support/silentLogger";
5
+ import { useShellSettled } from "../src/client/composables/useShellSettled.js";
6
+ import { SHELL_WEB_PLACEMENT_RUNTIME_KEY } from "../src/client/placement/inject.js";
7
+ import { createWebPlacementRuntime } from "../src/client/placement/runtime.js";
8
+ import { mountComposable } from "./support/mountComposable.js";
9
+
10
+ // Mounts the settle phase on a real placement runtime, listing each class it gives the shell's parts after the first.
11
+ function mountSettled(runtime) {
12
+ let settledNavigation = 0;
13
+ const { app, result } = mountComposable(() => useShellSettled({
14
+ settleNavigation() {
15
+ settledNavigation += 1;
16
+ }
17
+ }), { provide: [[SHELL_WEB_PLACEMENT_RUNTIME_KEY, runtime]] });
18
+ const classes = [];
19
+ watch(result.settlingClass, (value) => classes.push(value), { flush: "sync" });
20
+ return { app, classes, result, settledNavigation: () => settledNavigation };
21
+ }
22
+
23
+ // Lets every queued render and promise reaction run.
24
+ const drain = () => new Promise((resolve) => setTimeout(resolve, 0));
25
+
26
+ test("with nothing pending the shell settles as it mounts: unseen, then shown with transitions held, then itself", async () => {
27
+ const runtime = createWebPlacementRuntime({ components: new Map(), logger: silentLogger });
28
+ const { app, classes, result, settledNavigation } = mountSettled(runtime);
29
+ assert.equal(result.settlingClass.value, "shell-layout--waiting");
30
+ assert.equal(result.waiting.value, true);
31
+
32
+ await drain();
33
+
34
+ assert.deepEqual(classes, ["shell-layout--placing", ""]);
35
+ assert.equal(result.waiting.value, false);
36
+ assert.equal(settledNavigation(), 1);
37
+ app.unmount();
38
+ });
39
+
40
+ test("the shell waits while part of its context is pending, settles once it arrives, and waits only once", async () => {
41
+ const runtime = createWebPlacementRuntime({ components: new Map(), logger: silentLogger });
42
+ runtime.setContext({ consolePermissions: [] }, { source: "console-session", pending: true });
43
+ const { app, classes, result, settledNavigation } = mountSettled(runtime);
44
+
45
+ await drain();
46
+ assert.equal(result.waiting.value, true);
47
+ assert.deepEqual(classes, []);
48
+ assert.equal(settledNavigation(), 0);
49
+
50
+ runtime.setContext({ consolePermissions: ["builds.read"] }, { source: "console-session" });
51
+ await drain();
52
+ assert.deepEqual(classes, ["shell-layout--placing", ""]);
53
+ assert.equal(settledNavigation(), 1);
54
+
55
+ // A later wait redraws the navigation in place: the page stays shown.
56
+ runtime.setContext({ consolePermissions: [] }, { source: "console-session", pending: true });
57
+ await drain();
58
+ assert.equal(result.settlingClass.value, "");
59
+ assert.equal(settledNavigation(), 1);
60
+ app.unmount();
61
+ });
62
+
63
+ test("a shell unmounted while it waits never settles", async () => {
64
+ const runtime = createWebPlacementRuntime({ components: new Map(), logger: silentLogger });
65
+ runtime.setContext({ consolePermissions: [] }, { source: "console-session", pending: true });
66
+ const { app, classes, settledNavigation } = mountSettled(runtime);
67
+ await drain();
68
+
69
+ app.unmount();
70
+ runtime.setContext({ consolePermissions: ["builds.read"] }, { source: "console-session" });
71
+ await drain();
72
+
73
+ assert.deepEqual(classes, []);
74
+ assert.equal(settledNavigation(), 0);
75
+ });