@jskit-ai/shell-web 0.2.3 → 0.2.5

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.
Files changed (74) hide show
  1. package/fixtures/adaptive-shell/src/main.js +4 -2
  2. package/package.json +10 -4
  3. package/patterns/page-and-placement/example/src/placementTopology.js +1 -0
  4. package/src/client/asyncModuleRecovery/index.js +1 -0
  5. package/src/client/asyncModuleRecovery/inject.js +30 -18
  6. package/src/client/asyncModuleRecovery/runtime.js +20 -24
  7. package/src/client/bootstrap/bootstrapPayloadHandlerRegistry.js +13 -6
  8. package/src/client/browser.js +56 -0
  9. package/src/client/clientUpdate/runtime.js +28 -18
  10. package/src/client/components/ShellErrorHost.vue +19 -219
  11. package/src/client/components/ShellLayout.vue +19 -227
  12. package/src/client/components/ShellOutlet.vue +16 -53
  13. package/src/client/components/ShellRouteTransition.vue +2 -291
  14. package/src/client/composables/shellLayoutDrawerPreference.js +15 -8
  15. package/src/client/composables/useShellDrawer.js +194 -0
  16. package/src/client/composables/useShellErrorHost.js +171 -0
  17. package/src/client/composables/useShellLayoutState.js +10 -20
  18. package/src/client/composables/useShellNavigationLink.js +6 -6
  19. package/src/client/composables/useShellPullToRefresh.js +15 -19
  20. package/src/client/composables/useShellRouteTransition.js +218 -0
  21. package/src/client/error/index.js +1 -1
  22. package/src/client/error/inject.js +36 -48
  23. package/src/client/error/normalize.js +2 -3
  24. package/src/client/error/policy.js +9 -0
  25. package/src/client/error/presentation.js +67 -0
  26. package/src/client/error/presentationDefaults.js +2 -2
  27. package/src/client/error/presenters.js +44 -19
  28. package/src/client/error/runtime.js +30 -28
  29. package/src/client/error/store.js +4 -0
  30. package/src/client/navigation/linkResolver.js +6 -2
  31. package/src/client/navigation/usePaths.js +11 -8
  32. package/src/client/navigation/useSurfaceRouteContext.js +3 -3
  33. package/src/client/placement/debug.js +2 -2
  34. package/src/client/placement/index.js +1 -0
  35. package/src/client/placement/inject.js +49 -46
  36. package/src/client/placement/pathname.js +3 -9
  37. package/src/client/placement/runtime.js +13 -39
  38. package/src/client/placement/surfaceContext.js +14 -33
  39. package/src/client/placement/validators.js +6 -1
  40. package/src/client/providers/ShellWebClientProvider.js +17 -38
  41. package/src/client/providers/appModuleLoadFailure.js +1 -1
  42. package/src/client/requestRecovery/index.js +1 -0
  43. package/src/client/requestRecovery/inject.js +29 -17
  44. package/src/client/requestRecovery/runtime.js +40 -87
  45. package/src/client/runtime/bootstrapRuntime.js +20 -35
  46. package/src/client/runtime/refreshRuntime.js +30 -22
  47. package/src/client/stores/useShellErrorPresentationStore.js +5 -19
  48. package/src/client/support/drawerWidth.js +39 -0
  49. package/src/client/support/gestureTargets.js +4 -2
  50. package/src/client/support/menuLinkTarget.js +1 -1
  51. package/src/client/support/navigationLinkKeyboard.js +1 -1
  52. package/src/client/support/primaryNavigation.js +93 -0
  53. package/src/client/support/routeSwipeGesture.js +13 -3
  54. package/src/client/support/shellLayoutClass.js +4 -6
  55. package/src/client/types.js +38 -0
  56. package/test/asyncModuleRecoveryInject.test.js +13 -18
  57. package/test/bootstrapRuntime.test.js +3 -1
  58. package/test/browser.test.js +51 -0
  59. package/test/clientUpdate.test.js +12 -1
  60. package/test/drawerWidth.test.js +64 -0
  61. package/test/errorActions.test.js +10 -3
  62. package/test/errorPresentation.test.js +51 -0
  63. package/test/errorRuntime.test.js +21 -10
  64. package/test/placementInject.test.js +60 -0
  65. package/test/placementRuntime.test.js +12 -9
  66. package/test/primaryNavigation.test.js +75 -0
  67. package/test/provider.test.js +18 -4
  68. package/test/requestRecovery.test.js +13 -18
  69. package/test/settingsPlacementContract.test.js +15 -11
  70. package/test/support/mountComposable.js +90 -0
  71. package/test/translation.test.js +4 -2
  72. package/test/useShellDrawer.test.js +213 -0
  73. package/test/useShellErrorHost.test.js +149 -0
  74. package/test/useShellRouteTransition.test.js +129 -0
@@ -0,0 +1,93 @@
1
+ import { normalizePathname } from "@jskit-ai/kernel/shared";
2
+ import { resolveSurfaceNavigationTargetFromPlacementContext } from "../placement/surfaceContext.js";
3
+ import {
4
+ normalizeMenuLinkPathname,
5
+ resolveMenuLinkTarget
6
+ } from "./menuLinkTarget.js";
7
+
8
+ function normalizeComparablePathname(value = "") {
9
+ const raw = String(value || "").trim();
10
+ if (!raw) {
11
+ return "";
12
+ }
13
+
14
+ try {
15
+ const parsed = new URL(raw, window.location.href);
16
+ if (parsed.origin !== window.location.origin) {
17
+ return "";
18
+ }
19
+ return normalizePathname(parsed.pathname);
20
+ } catch {
21
+ return normalizePathname(normalizeMenuLinkPathname(raw));
22
+ }
23
+ }
24
+
25
+ function pathMatchesNavigationEntry(pathname = "", entry = {}) {
26
+ const target = String(entry.pathname || "").trim();
27
+ if (!target) {
28
+ return false;
29
+ }
30
+ if (entry.exact || target === "/") {
31
+ return pathname === target;
32
+ }
33
+ return pathname === target || pathname.startsWith(`${target}/`);
34
+ }
35
+
36
+ // The primary navigation destinations a swipe can reach: same-origin links, in placement order.
37
+ function resolvePrimaryNavigationEntries(placements, {
38
+ semanticTarget,
39
+ currentSurfaceId,
40
+ placementContext,
41
+ routeParams
42
+ }) {
43
+ return placements
44
+ .filter((entry) => String(entry.semanticTarget || entry.target || "").trim() === semanticTarget)
45
+ .map((entry, index) => {
46
+ const entryProps = entry.props;
47
+ const target = resolveMenuLinkTarget({
48
+ to: entryProps.to,
49
+ surface: entryProps.surface,
50
+ currentSurfaceId,
51
+ placementContext,
52
+ scopedSuffix: entryProps.scopedSuffix,
53
+ unscopedSuffix: entryProps.unscopedSuffix,
54
+ routeParams
55
+ });
56
+ const navigationTarget = resolveSurfaceNavigationTargetFromPlacementContext(placementContext, {
57
+ path: target,
58
+ surfaceId: entryProps.surface || currentSurfaceId
59
+ });
60
+
61
+ return Object.freeze({
62
+ id: entry.id,
63
+ index,
64
+ exact: entryProps.exact === true,
65
+ href: navigationTarget.href,
66
+ pathname: normalizeComparablePathname(navigationTarget.href),
67
+ sameOrigin: navigationTarget.sameOrigin
68
+ });
69
+ })
70
+ .filter((entry) => entry.pathname && entry.sameOrigin);
71
+ }
72
+
73
+ // The destination whose path matches most closely, or -1 away from all of them.
74
+ function resolveActivePrimaryNavigationIndex(pathname, entries) {
75
+ let bestMatch = null;
76
+
77
+ for (const entry of entries) {
78
+ if (!pathMatchesNavigationEntry(pathname, entry)) {
79
+ continue;
80
+ }
81
+ if (!bestMatch || entry.pathname.length > bestMatch.pathname.length) {
82
+ bestMatch = entry;
83
+ }
84
+ }
85
+
86
+ return bestMatch ? bestMatch.index : -1;
87
+ }
88
+
89
+ export {
90
+ normalizeComparablePathname,
91
+ resolveActivePrimaryNavigationIndex,
92
+ resolvePrimaryNavigationEntries
93
+ };
@@ -15,8 +15,12 @@ function isVerticalDrift(absX, absY) {
15
15
  return absY > SWIPE_MAX_VERTICAL_DRIFT || (absY > 28 && absY > absX * 1.3);
16
16
  }
17
17
 
18
- // Tracks one horizontal swipe between primary navigation destinations. A swipe
19
- // that drifts vertically is dropped for good: the gesture became a scroll.
18
+ /**
19
+ * Tracks one horizontal swipe between primary navigation destinations. A swipe
20
+ * that drifts vertically is dropped for good: the gesture became a scroll.
21
+ * @param {object} [options]
22
+ * @param {() => number} [options.now]
23
+ */
20
24
  function createRouteSwipeGesture({ now = () => performance.now() } = {}) {
21
25
  let active = null;
22
26
 
@@ -82,7 +86,13 @@ function createRouteSwipeGesture({ now = () => performance.now() } = {}) {
82
86
  }
83
87
  }
84
88
 
85
- return Object.freeze({ start, reset, move, end, cancel });
89
+ return Object.freeze({
90
+ start,
91
+ reset,
92
+ move,
93
+ end,
94
+ cancel
95
+ });
86
96
  }
87
97
 
88
98
  export { createRouteSwipeGesture };
@@ -1,9 +1,11 @@
1
+ import { pageViewportWidth } from "../browser.js";
2
+
1
3
  const COMPACT_MAX_WIDTH = 600;
2
4
  const MEDIUM_MAX_WIDTH = 1280;
3
5
 
4
6
  // Maps the Vuetify display breakpoint name to the shell layout class. Without
5
7
  // a known breakpoint the viewport width decides.
6
- function resolveShellLayoutClass(displayName = "", viewportWidth = 0) {
8
+ function resolveShellLayoutClass(displayName = "", viewportWidth = pageViewportWidth()) {
7
9
  const name = String(displayName || "").trim().toLowerCase();
8
10
  if (name === "xs" || name === "sm") {
9
11
  return "compact";
@@ -25,8 +27,4 @@ function resolveShellLayoutClass(displayName = "", viewportWidth = 0) {
25
27
  return "expanded";
26
28
  }
27
29
 
28
- function readViewportWidth() {
29
- return typeof window === "object" ? Number(window.innerWidth || 0) : 0;
30
- }
31
-
32
- export { readViewportWidth, resolveShellLayoutClass };
30
+ export { resolveShellLayoutClass };
@@ -0,0 +1,38 @@
1
+ // The values shell-web's client hands out and receives across files, for its own modules and for the packages that use
2
+ // the shell.
3
+
4
+ /** @typedef {ReturnType<typeof import("./error/runtime.js").createErrorRuntime>} ShellErrorRuntime The error runtime. */
5
+ /** @typedef {ReturnType<typeof import("./error/store.js").createErrorPresentationStore>} ShellErrorPresentationStore What the error host shows. */
6
+ /** @typedef {ReturnType<typeof import("./placement/runtime.js").createWebPlacementRuntime>} WebPlacementRuntime The placement runtime. */
7
+ /** @typedef {ReturnType<typeof import("./asyncModuleRecovery/runtime.js").createShellAsyncModuleRecoveryRuntime>} ShellAsyncModuleRecoveryRuntime Offers a reload when an application module fails to load. */
8
+ /** @typedef {ReturnType<typeof import("./requestRecovery/runtime.js").createShellRequestRecoveryRuntime>} ShellRequestRecoveryRuntime Offers a retry or a reload when a request cannot reach the server. */
9
+ /** @typedef {ReturnType<typeof import("./runtime/bootstrapRuntime.js").createShellBootstrapRuntime>} ShellBootstrapRuntime Fetches and applies the bootstrap payload. */
10
+ /** @typedef {ReturnType<typeof import("./runtime/refreshRuntime.js").createShellRefreshRuntime>} ShellRefreshRuntime Refreshes the bootstrap payload and the active queries. */
11
+ /** @typedef {ReturnType<typeof import("./bootstrap/bootstrapPayloadHandlerRegistry.js").createBootstrapPayloadHandlerRegistry>} BootstrapPayloadHandlerRegistry The bootstrap payload handlers. */
12
+ /** @typedef {ReturnType<typeof import("./navigation/useSurfaceRouteContext.js").useSurfaceRouteContext>} SurfaceRouteContext The current route and the surface it is on. */
13
+
14
+ /**
15
+ * The part of the optional `client.realtime` capability the shell reads.
16
+ * @typedef {object} ShellRealtime
17
+ * @property {(listener: (buildId: string) => void) => () => void} onServedClientBuild Calls the listener with each client build a server names; returns the unsubscribe.
18
+ */
19
+
20
+ /**
21
+ * What `useShellLayoutState()` reads from a layout's props.
22
+ * @typedef {object} ShellLayoutStateProps
23
+ * @property {string} [surface] The surface, when the layout names one instead of following the route.
24
+ * @property {string | { key: string, fallback?: string }} [surfaceLabel] A message key, `{ key, fallback }` or plain text.
25
+ */
26
+
27
+ /**
28
+ * The props of a surface-aware menu or tab link.
29
+ * @typedef {object} ShellNavigationLinkProps
30
+ * @property {string | { key: string, fallback?: string }} label A message key, `{ key, fallback }` or plain text.
31
+ * @property {string} to
32
+ * @property {string} icon
33
+ * @property {string} surface
34
+ * @property {string} scopedSuffix
35
+ * @property {string} unscopedSuffix
36
+ */
37
+
38
+ export {};
@@ -2,12 +2,23 @@ import assert from "node:assert/strict";
2
2
  import test from "node:test";
3
3
  import { createApp } from "vue";
4
4
  import {
5
+ NO_SHELL_ASYNC_MODULE_RECOVERY_RUNTIME,
5
6
  SHELL_ASYNC_MODULE_RECOVERY_RUNTIME_KEY,
6
7
  useShellAsyncModuleRecoveryRuntime
7
8
  } from "../src/client/asyncModuleRecovery/index.js";
8
9
 
9
- test("shell async module recovery runtime composable returns null outside Vue injection context", () => {
10
- assert.equal(useShellAsyncModuleRecoveryRuntime(), null);
10
+ test("shell async module recovery runtime composable resolves NO_SHELL_ASYNC_MODULE_RECOVERY_RUNTIME without the provider", async () => {
11
+ const app = createApp({
12
+ render() {
13
+ return null;
14
+ }
15
+ });
16
+
17
+ const runtime = app.runWithContext(() => useShellAsyncModuleRecoveryRuntime());
18
+
19
+ assert.equal(runtime, NO_SHELL_ASYNC_MODULE_RECOVERY_RUNTIME);
20
+ assert.equal(runtime.notify(new Error("failed")), null);
21
+ assert.equal(await runtime.reload(), false);
11
22
  });
12
23
 
13
24
  test("shell async module recovery runtime composable resolves the provided public runtime", async () => {
@@ -31,19 +42,3 @@ test("shell async module recovery runtime composable resolves the provided publi
31
42
  runtime
32
43
  );
33
44
  });
34
-
35
- test("shell async module recovery runtime composable rejects incomplete runtimes", () => {
36
- const app = createApp({
37
- render() {
38
- return null;
39
- }
40
- });
41
- app.provide(SHELL_ASYNC_MODULE_RECOVERY_RUNTIME_KEY, {
42
- notify() {}
43
- });
44
-
45
- assert.equal(
46
- app.runWithContext(() => useShellAsyncModuleRecoveryRuntime()),
47
- null
48
- );
49
- });
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
2
2
  import test from "node:test";
3
3
  import { createBootstrapPayloadHandlerRegistry } from "../src/client/bootstrap/bootstrapPayloadHandlerRegistry.js";
4
4
  import { createShellBootstrapRuntime } from "../src/client/runtime/bootstrapRuntime.js";
5
+ import { silentLogger } from "@jskit-ai/kernel/shared/support/silentLogger";
5
6
 
6
7
  function createPlacementRuntime(initialContext = {}) {
7
8
  let context = Object.freeze({ ...initialContext });
@@ -100,7 +101,8 @@ test("shell bootstrap runtime refreshes /api/bootstrap on init and applies regis
100
101
  return payload;
101
102
  }
102
103
  };
103
- }
104
+ },
105
+ logger: silentLogger
104
106
  });
105
107
 
106
108
  await runtime.initialize();
@@ -0,0 +1,51 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import {
4
+ closestElement,
5
+ pageDocument,
6
+ pageOrigin,
7
+ pagePathname,
8
+ pageStorage,
9
+ pageTitle,
10
+ pageViewportWidth,
11
+ pageWindow
12
+ } from "../src/client/browser.js";
13
+
14
+ test("outside a browser every page read is empty", () => {
15
+ assert.equal(pageWindow(), null);
16
+ assert.equal(pageDocument(), null);
17
+ assert.equal(pagePathname(), "");
18
+ assert.equal(pageOrigin(), "");
19
+ assert.equal(pageTitle(), "");
20
+ assert.equal(pageViewportWidth(), 0);
21
+ assert.equal(pageStorage(), null);
22
+ });
23
+
24
+ test("in a browser the page reads come from the window and the document", (t) => {
25
+ const localStorage = {};
26
+ globalThis.window = {
27
+ innerWidth: 390,
28
+ localStorage,
29
+ location: { pathname: " /home ", origin: "https://app.test" }
30
+ };
31
+ globalThis.document = { title: "App" };
32
+ t.after(() => {
33
+ delete globalThis.window;
34
+ delete globalThis.document;
35
+ });
36
+
37
+ assert.equal(pagePathname(), "/home");
38
+ assert.equal(pageOrigin(), "https://app.test");
39
+ assert.equal(pageTitle(), "App");
40
+ assert.equal(pageViewportWidth(), 390);
41
+ assert.equal(pageStorage(), localStorage);
42
+ });
43
+
44
+ test("an event target that is not an element has no closest element", () => {
45
+ const element = {};
46
+
47
+ assert.equal(closestElement({ closest: () => element }, "a"), element);
48
+ assert.equal(closestElement({ closest: () => null }, "a"), null);
49
+ assert.equal(closestElement({}, "a"), null);
50
+ assert.equal(closestElement(null, "a"), null);
51
+ });
@@ -92,7 +92,18 @@ async function startClient(t, { inputs = {}, providers = [], probe = { shell: "c
92
92
  "client.env": {},
93
93
  "client.logger": logger,
94
94
  "client.pinia": createPinia(),
95
- "client.query": null,
95
+ "client.query": {
96
+ getQueryCache() {
97
+ return {
98
+ getAll() {
99
+ return [];
100
+ },
101
+ subscribe() {
102
+ return () => {};
103
+ }
104
+ };
105
+ }
106
+ },
96
107
  "client.router": { currentRoute: { value: { path: "/" } }, onError: () => () => {} },
97
108
  "client.surface": createSurfaceRuntime({
98
109
  allMode: "all",
@@ -4,6 +4,7 @@ import {
4
4
  DEFAULT_SHELL_DRAWER_WIDTH,
5
5
  DEFAULT_SHELL_NAVIGATION_ITEM_SPACING,
6
6
  DEFAULT_SHELL_RAIL_WIDTH,
7
+ measureDrawerContentWidth,
7
8
  normalizeShellDrawerWidth,
8
9
  normalizeShellNavigationItemSpacing,
9
10
  normalizeShellRailWidth,
@@ -57,3 +58,66 @@ test("content-aware width ignores invalid measurements and uses a bounded fallba
57
58
  360
58
59
  );
59
60
  });
61
+
62
+ function installMeasuredPage(t, { drawerStyle, textRects }) {
63
+ globalThis.window = {
64
+ getComputedStyle: (element) => element.style || drawerStyle
65
+ };
66
+ globalThis.document = {
67
+ createRange() {
68
+ let rect = null;
69
+ return {
70
+ selectNodeContents(element) {
71
+ rect = textRects.get(element);
72
+ },
73
+ getBoundingClientRect: () => rect
74
+ };
75
+ }
76
+ };
77
+ t.after(() => {
78
+ delete globalThis.window;
79
+ delete globalThis.document;
80
+ });
81
+ }
82
+
83
+ function createLabel(style = {}) {
84
+ return { style: { display: "block", visibility: "visible", ...style } };
85
+ }
86
+
87
+ test("the drawer measures the labels people can see, from its inline start", (t) => {
88
+ const wide = createLabel();
89
+ const hidden = createLabel({ visibility: "hidden" });
90
+ const collapsed = createLabel({ display: "none" });
91
+ const empty = createLabel();
92
+ installMeasuredPage(t, {
93
+ drawerStyle: { direction: "ltr", borderRightWidth: "1px", borderLeftWidth: "9px" },
94
+ textRects: new Map([
95
+ [wide, { left: 56, right: 156, width: 100 }],
96
+ [hidden, { left: 56, right: 356, width: 300 }],
97
+ [collapsed, { left: 56, right: 356, width: 300 }],
98
+ [empty, { left: 56, right: 56, width: 0 }]
99
+ ])
100
+ });
101
+
102
+ const width = measureDrawerContentWidth({
103
+ getBoundingClientRect: () => ({ left: 40, right: 288 }),
104
+ querySelectorAll: () => [wide, hidden, collapsed, empty]
105
+ }, 12);
106
+
107
+ assert.equal(width, 16 + 100 + 12 + 1);
108
+ });
109
+
110
+ test("a right-to-left drawer measures from its right edge and ends at its left border", (t) => {
111
+ const label = createLabel();
112
+ installMeasuredPage(t, {
113
+ drawerStyle: { direction: "rtl", borderRightWidth: "9px", borderLeftWidth: "1px" },
114
+ textRects: new Map([[label, { left: 148, right: 248, width: 100 }]])
115
+ });
116
+
117
+ const width = measureDrawerContentWidth({
118
+ getBoundingClientRect: () => ({ left: 0, right: 264 }),
119
+ querySelectorAll: () => [label]
120
+ }, 12);
121
+
122
+ assert.equal(width, 16 + 100 + 12 + 1);
123
+ });
@@ -3,13 +3,14 @@ import test from "node:test";
3
3
  import { readFileSync } from "node:fs";
4
4
  import { registerHooks } from "node:module";
5
5
  import { compileScript, parse } from "@vue/compiler-sfc";
6
- import { createRenderer, defineComponent, h, nextTick } from "vue";
6
+ import { createRenderer, defineComponent, h, nextTick, shallowRef } from "vue";
7
7
  import { createPinia } from "pinia";
8
8
  import { createErrorRuntime } from "../src/client/error/runtime.js";
9
9
  import { SHELL_WEB_ERROR_RUNTIME_KEY } from "../src/client/error/inject.js";
10
10
  import { createDefaultMaterialErrorPresenters } from "../src/client/error/presenters.js";
11
11
  import { createErrorPresentationStore } from "../src/client/error/store.js";
12
12
  import { useShellErrorPresentationStore } from "../src/client/stores/useShellErrorPresentationStore.js";
13
+ import { silentLogger } from "@jskit-ai/kernel/shared/support/silentLogger";
13
14
 
14
15
  const componentUrl = new URL("../src/client/components/ShellErrorHost.vue", import.meta.url).href;
15
16
  const hooks = registerHooks({
@@ -43,20 +44,26 @@ function fixture(t) {
43
44
  const store = createErrorPresentationStore();
44
45
  const runtime = createErrorRuntime({
45
46
  presenters: createDefaultMaterialErrorPresenters({ store }),
46
- defaultPresenterId: "material.snackbar"
47
+ defaultPresenterId: "material.snackbar",
48
+ logger: silentLogger
47
49
  });
48
50
  const app = renderer.createApp(ShellErrorHost);
49
51
  const pinia = createPinia();
50
52
  app.use(pinia);
51
53
  useShellErrorPresentationStore(pinia).attachRuntimeStore(store);
52
54
  app.provide(SHELL_WEB_ERROR_RUNTIME_KEY, runtime);
55
+ app.provide(Symbol.for("vuetify:layout"), { mainRect: shallowRef({ top: 0, left: 0, right: 0, bottom: 0 }) });
56
+ globalThis.document = { documentElement: { style: { removeProperty() {} } } };
53
57
  for (const name of ["VSnackbar", "VDialog", "VCard", "VCardTitle", "VCardText", "VCardActions", "VSpacer", "VAlert", "VBtn"]) {
54
58
  app.component(name, defineComponent({ setup: (_props, { slots }) => () =>
55
59
  h(name, {}, [slots.default?.(), slots.actions?.()]) }));
56
60
  }
57
61
  const root = node("root");
58
62
  app.mount(root);
59
- t.after(() => app.unmount());
63
+ t.after(() => {
64
+ app.unmount();
65
+ delete globalThis.document;
66
+ });
60
67
  const all = (item = root) => [item, ...item.children.flatMap((child) => all(child))];
61
68
  const button = (label) => all().find((item) => item.type === "VBtn" &&
62
69
  all(item).some((child) => child.text.trim() === label));
@@ -0,0 +1,51 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { mdiAlertOutline, mdiCheckCircleOutline, mdiInformationOutline } from "@mdi/js";
4
+ import {
5
+ actionsFor,
6
+ resolveBannerPlacement,
7
+ resolveSeverityColor,
8
+ resolveSeverityIcon,
9
+ resolveTimeout
10
+ } from "../src/client/error/presentation.js";
11
+
12
+ test("each severity has its color and icon, and anything else reads as an error", () => {
13
+ assert.equal(resolveSeverityColor(" INFO "), "info");
14
+ assert.equal(resolveSeverityColor("success"), "success");
15
+ assert.equal(resolveSeverityColor("warning"), "warning");
16
+ assert.equal(resolveSeverityColor("critical"), "error");
17
+ assert.equal(resolveSeverityColor(""), "error");
18
+
19
+ assert.equal(resolveSeverityIcon("info"), mdiInformationOutline);
20
+ assert.equal(resolveSeverityIcon("success"), mdiCheckCircleOutline);
21
+ assert.equal(resolveSeverityIcon("warning"), mdiAlertOutline);
22
+ assert.equal(resolveSeverityIcon("error"), mdiAlertOutline);
23
+ });
24
+
25
+ test("a snackbar times out unless it persists or shows nothing", () => {
26
+ assert.equal(resolveTimeout(null), -1);
27
+ assert.equal(resolveTimeout({ persist: true }), -1);
28
+ assert.equal(resolveTimeout({ persist: false }), 5000);
29
+ });
30
+
31
+ test("an entry's actions list its action first and skip an empty one", () => {
32
+ const action = { label: "Retry" };
33
+ const additional = { label: "Details" };
34
+
35
+ assert.deepEqual(actionsFor(null), []);
36
+ assert.deepEqual(actionsFor({ action, additionalActions: [additional] }), [action, additional]);
37
+ assert.deepEqual(actionsFor({ action: null, additionalActions: [additional] }), [additional]);
38
+ });
39
+
40
+ test("banners float below a top app bar and join the page without one", () => {
41
+ assert.deepEqual(resolveBannerPlacement({ top: 0 }), { inline: true, style: undefined });
42
+ assert.deepEqual(resolveBannerPlacement({}), { inline: true, style: undefined });
43
+ assert.deepEqual(resolveBannerPlacement({ top: 64, left: 256, right: 0 }), {
44
+ inline: false,
45
+ style: {
46
+ top: "calc(env(safe-area-inset-top, 0px) + 64px)",
47
+ left: "256px",
48
+ right: "0px"
49
+ }
50
+ });
51
+ });
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
2
2
  import test from "node:test";
3
3
  import { createDefaultErrorPolicy } from "../src/client/error/policy.js";
4
4
  import { createErrorRuntime } from "../src/client/error/runtime.js";
5
+ import { silentLogger } from "@jskit-ai/kernel/shared/support/silentLogger";
5
6
 
6
7
  function createPresenter(id, {
7
8
  channels = ["snackbar", "banner", "dialog"],
@@ -47,7 +48,8 @@ test("error runtime treats resource load errors as silent by default", () => {
47
48
  presenters: [
48
49
  createPresenter("module.presenter", { calls })
49
50
  ],
50
- moduleDefaultPresenterId: "module.presenter"
51
+ moduleDefaultPresenterId: "module.presenter",
52
+ logger: silentLogger
51
53
  });
52
54
 
53
55
  const result = runtime.report({
@@ -75,7 +77,8 @@ test("error runtime prefers policy presenter over app and module defaults", () =
75
77
  createPresenter("app.presenter", { calls }),
76
78
  createPresenter("policy.presenter", { calls })
77
79
  ],
78
- moduleDefaultPresenterId: "module.presenter"
80
+ moduleDefaultPresenterId: "module.presenter",
81
+ logger: silentLogger
79
82
  });
80
83
 
81
84
  runtime.configure({
@@ -104,7 +107,8 @@ test("error runtime uses app default presenter when policy omits presenter", ()
104
107
  createPresenter("module.presenter", { calls }),
105
108
  createPresenter("app.presenter", { calls })
106
109
  ],
107
- moduleDefaultPresenterId: "module.presenter"
110
+ moduleDefaultPresenterId: "module.presenter",
111
+ logger: silentLogger
108
112
  });
109
113
 
110
114
  runtime.configure({
@@ -135,7 +139,8 @@ test("error runtime falls back to module default presenter", () => {
135
139
  policy: () => ({
136
140
  channel: "snackbar",
137
141
  message: "Module default"
138
- })
142
+ }),
143
+ logger: silentLogger
139
144
  });
140
145
 
141
146
  const result = runtime.report({
@@ -153,7 +158,8 @@ test("error runtime fails fast when module default presenter is unresolved", ()
153
158
  () =>
154
159
  createErrorRuntime({
155
160
  presenters: [createPresenter("registered.presenter")],
156
- moduleDefaultPresenterId: "missing.presenter"
161
+ moduleDefaultPresenterId: "missing.presenter",
162
+ logger: silentLogger
157
163
  }),
158
164
  /Module default error presenter "missing.presenter" is not registered/,
159
165
  );
@@ -162,7 +168,8 @@ test("error runtime fails fast when module default presenter is unresolved", ()
162
168
  test("error runtime fails fast when app default presenter is unresolved", () => {
163
169
  const runtime = createErrorRuntime({
164
170
  presenters: [createPresenter("module.presenter")],
165
- moduleDefaultPresenterId: "module.presenter"
171
+ moduleDefaultPresenterId: "module.presenter",
172
+ logger: silentLogger
166
173
  });
167
174
 
168
175
  assert.throws(
@@ -179,7 +186,8 @@ test("error runtime throws when policy presenter is unknown", () => {
179
186
  channel: "dialog",
180
187
  presenterId: "missing.presenter",
181
188
  message: "failure"
182
- })
189
+ }),
190
+ logger: silentLogger
183
191
  });
184
192
 
185
193
  assert.throws(
@@ -194,7 +202,8 @@ test("error runtime falls back to a channel-compatible presenter when default pr
194
202
  createPresenter("module.presenter"),
195
203
  createPresenter("app.presenter", { channels: ["banner"] })
196
204
  ],
197
- moduleDefaultPresenterId: "module.presenter"
205
+ moduleDefaultPresenterId: "module.presenter",
206
+ logger: silentLogger
198
207
  });
199
208
 
200
209
  runtime.configure({
@@ -215,7 +224,8 @@ test("error runtime throws when no presenter supports channel", () => {
215
224
  createPresenter("module.presenter", { channels: ["snackbar"] }),
216
225
  createPresenter("app.presenter", { channels: ["banner"] })
217
226
  ],
218
- moduleDefaultPresenterId: "module.presenter"
227
+ moduleDefaultPresenterId: "module.presenter",
228
+ logger: silentLogger
219
229
  });
220
230
 
221
231
  runtime.configure({
@@ -238,7 +248,8 @@ test("error runtime honours an explicit persist on any channel and defaults by c
238
248
  const store = createErrorPresentationStore({ now: () => 1000 });
239
249
  const runtime = createErrorRuntime({
240
250
  presenters: createDefaultMaterialErrorPresenters({ store }),
241
- moduleDefaultPresenterId: "material.snackbar"
251
+ moduleDefaultPresenterId: "material.snackbar",
252
+ logger: silentLogger
242
253
  });
243
254
 
244
255
  const persistentSnackbar = runtime.report({ message: "Keep me", channel: "snackbar", persist: true });
@@ -0,0 +1,60 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import {
4
+ NO_WEB_PLACEMENT_RUNTIME,
5
+ SHELL_WEB_PLACEMENT_RUNTIME_KEY,
6
+ useWebPlacementContext,
7
+ useWebPlacementRevision,
8
+ useWebPlacementRuntime
9
+ } from "../src/client/placement/inject.js";
10
+ import { createWebPlacementRuntime } from "../src/client/placement/runtime.js";
11
+ import { silentLogger } from "@jskit-ai/kernel/shared/support/silentLogger";
12
+ import { mountComposable } from "./support/mountComposable.js";
13
+
14
+ function mountWithRuntime(useComposable) {
15
+ const runtime = createWebPlacementRuntime({ components: new Map(), logger: silentLogger });
16
+ return { runtime, ...mountComposable(useComposable(runtime), { provide: [[SHELL_WEB_PLACEMENT_RUNTIME_KEY, runtime]] }) };
17
+ }
18
+
19
+ test("an application without the provider places nothing", () => {
20
+ const { app, result } = mountComposable(() => ({
21
+ runtime: useWebPlacementRuntime(),
22
+ placement: useWebPlacementContext()
23
+ }));
24
+
25
+ assert.equal(result.runtime, NO_WEB_PLACEMENT_RUNTIME);
26
+ assert.deepEqual(result.runtime.getPlacements(), []);
27
+ assert.deepEqual(result.placement.context.value, {});
28
+ assert.deepEqual(result.placement.mergeContext({ a: 1 }), {});
29
+ app.unmount();
30
+ });
31
+
32
+ test("the placement context follows the runtime while mounted", () => {
33
+ const { app, result, runtime } = mountWithRuntime(() => useWebPlacementContext);
34
+
35
+ runtime.setContext({ surface: "app" });
36
+ assert.equal(result.context.value.surface, "app");
37
+
38
+ result.mergeContext({ extra: true }, "test");
39
+ assert.deepEqual(runtime.getContext(), { surface: "app", extra: true });
40
+
41
+ result.replaceContext({ only: true }, "test");
42
+ assert.deepEqual(runtime.getContext(), { only: true });
43
+
44
+ app.unmount();
45
+ runtime.setContext({ after: true });
46
+ assert.equal(result.context.value.after, undefined);
47
+ });
48
+
49
+ test("the placement revision follows the runtime while mounted", () => {
50
+ const { app, result, runtime } = mountWithRuntime((target) => () => useWebPlacementRevision(target));
51
+ assert.equal(result.value, 0);
52
+
53
+ runtime.setContext({ surface: "app" });
54
+ assert.equal(result.value, runtime.getRevision());
55
+
56
+ app.unmount();
57
+ const stopped = result.value;
58
+ runtime.setContext({ surface: "admin" });
59
+ assert.equal(result.value, stopped);
60
+ });