@terpjs/react-core 0.30.0 → 0.32.0

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 (82) hide show
  1. package/README.md +35 -14
  2. package/package.json +5 -5
  3. package/src/DashboardPage.tsx +34 -0
  4. package/src/Field.test.tsx +26 -1
  5. package/src/Field.tsx +15 -5
  6. package/src/HubPage.tsx +7 -2
  7. package/src/ModuleNav.test.tsx +29 -1
  8. package/src/ModuleNav.tsx +5 -1
  9. package/src/Page.test.tsx +192 -0
  10. package/src/Page.tsx +141 -55
  11. package/src/PageActions.test.tsx +64 -3
  12. package/src/PageActions.tsx +40 -15
  13. package/src/PageSequence.tsx +114 -0
  14. package/src/Stat.test.tsx +376 -0
  15. package/src/Stat.tsx +316 -0
  16. package/src/Timeline.test.tsx +74 -0
  17. package/src/Timeline.tsx +71 -0
  18. package/src/admin/AdminHub.tsx +233 -78
  19. package/src/admin/AuditLogAdmin.tsx +3 -1
  20. package/src/admin/GroupCreate.tsx +3 -1
  21. package/src/admin/GroupDetail.tsx +72 -67
  22. package/src/admin/GroupsAdmin.tsx +3 -1
  23. package/src/admin/ModuleAccessPanel.tsx +13 -12
  24. package/src/admin/RecordHistory.tsx +88 -0
  25. package/src/admin/UserCreate.tsx +3 -1
  26. package/src/admin/UserDetail.tsx +8 -1
  27. package/src/admin/UsersAdmin.tsx +3 -0
  28. package/src/admin/admin.test.tsx +389 -19
  29. package/src/bootstrap.test.tsx +31 -7
  30. package/src/bootstrap.tsx +30 -4
  31. package/src/charts/BarChart.tsx +74 -0
  32. package/src/charts/ProportionBar.tsx +118 -0
  33. package/src/charts/StatusHistory.tsx +75 -0
  34. package/src/charts/TrendChart.tsx +232 -0
  35. package/src/charts/charts.test.tsx +409 -0
  36. package/src/charts/scale.ts +103 -0
  37. package/src/currentInView.ts +28 -0
  38. package/src/dataview/DataView.test.tsx +303 -1
  39. package/src/dataview/DataView.tsx +53 -3
  40. package/src/dataview/DataViewCardList.tsx +21 -13
  41. package/src/dataview/DataViewTable.tsx +5 -6
  42. package/src/dataview/DataViewToolbar.tsx +4 -1
  43. package/src/dataview/README.md +18 -0
  44. package/src/dataview/index.ts +1 -0
  45. package/src/dataview/internal.tsx +112 -5
  46. package/src/dataview/types.ts +39 -10
  47. package/src/errorMessages.test.tsx +17 -0
  48. package/src/errorMessages.tsx +18 -0
  49. package/src/format.ts +15 -0
  50. package/src/index.ts +24 -1
  51. package/src/layout.manifest.json +2 -9
  52. package/src/layout.manifest.test.ts +4 -2
  53. package/src/layout.test.tsx +74 -6
  54. package/src/layout.tsx +85 -25
  55. package/src/layoutContract.test.tsx +239 -1
  56. package/src/layoutContract.ts +131 -5
  57. package/src/layoutDeclaration.test.ts +31 -10
  58. package/src/layoutDeclaration.ts +9 -4
  59. package/src/locale.tsx +28 -3
  60. package/src/markers.test.ts +70 -2
  61. package/src/router.test.tsx +15 -3
  62. package/src/router.tsx +11 -2
  63. package/src/ssr.test.tsx +18 -0
  64. package/src/styles.test.ts +288 -190
  65. package/src/styles.ts +1379 -361
  66. package/src/theme.test.tsx +44 -18
  67. package/src/theme.themes.test.ts +22 -2
  68. package/src/theme.tsx +22 -14
  69. package/src/themes.ts +39 -4
  70. package/src/tokens.guard.test.ts +13 -15
  71. package/src/typography.tsx +5 -1
  72. package/src/ui/Alert.test.tsx +24 -1
  73. package/src/ui/Alert.tsx +10 -1
  74. package/src/ui/Card.tsx +5 -4
  75. package/src/ui/Tabs.test.tsx +44 -0
  76. package/src/ui/Tabs.tsx +5 -2
  77. package/src/ui/Tooltip.test.tsx +171 -2
  78. package/src/ui/Tooltip.tsx +144 -8
  79. package/src/ui/controlMessages.tsx +20 -7
  80. package/src/uiText.test.tsx +16 -0
  81. package/src/uiText.tsx +105 -7
  82. package/src/viewerTimeZone.ts +15 -0
package/src/layout.tsx CHANGED
@@ -108,21 +108,20 @@ export type GridColumns = 1 | 2 | 3 | 4 | "auto";
108
108
  /** Track floor for an `"auto"` grid: the width below which a column stops being one. */
109
109
  export type GridMinColumn = "xs" | "sm" | "md" | "lg";
110
110
 
111
- export interface GridProps extends Omit<HTMLAttributes<HTMLElement>, "style"> {
111
+ /**
112
+ * A named track template (ADR 0169 §2): the tracks' proportions, read left to right.
113
+ *
114
+ * `"2:1"` is a main track twice the width of the one beside it, `"1:2"` the same with the
115
+ * narrow track first, `"3:1"` a main track with a narrow rail; the equal sets are two, three
116
+ * and four tracks of one width. Every template collapses at the framework's one cutover: to a
117
+ * single track, and the four-track set to two, so a row of four small blocks becomes a square
118
+ * on a phone rather than a column four screens long.
119
+ */
120
+ export type GridTemplate = "1:1" | "1:1:1" | "1:1:1:1" | "2:1" | "1:2" | "3:1";
121
+
122
+ interface GridBaseProps extends Omit<HTMLAttributes<HTMLElement>, "style"> {
112
123
  /** The rendered element (`"div"` by default; use `"ul"`, `"section"`, `"dl"`, …). */
113
124
  as?: ElementType;
114
- /**
115
- * Columns: a fixed count, or `"auto"` (the default) for as many as fit above
116
- * {@link GridProps.minColumn}.
117
- *
118
- * `"auto"` is the responsive answer and it takes no breakpoint: the track floor makes the
119
- * grid reflow to whatever its **container** can hold, which is what a caller almost always
120
- * means and is more nearly right than a viewport query — a grid inside a narrow panel
121
- * should go one-column whatever the window is doing.
122
- */
123
- columns?: GridColumns;
124
- /** Track floor for an `"auto"` grid (default `"sm"`); ignored at a fixed count. */
125
- minColumn?: GridMinColumn;
126
125
  /** Gap between cells, as a step on the token spacing scale (default `4`). */
127
126
  gap?: SpaceToken;
128
127
  /** Inset around the cells, as a step on the token spacing scale (default none). */
@@ -136,6 +135,41 @@ export interface GridProps extends Omit<HTMLAttributes<HTMLElement>, "style"> {
136
135
  children?: ReactNode;
137
136
  }
138
137
 
138
+ /** How a grid's tracks are decided: by count and floor, or by a named template — never both. */
139
+ type GridTracks =
140
+ | {
141
+ /**
142
+ * Columns: a fixed count, or `"auto"` (the default) for as many as fit above
143
+ * `minColumn`.
144
+ *
145
+ * `"auto"` is the responsive answer and it takes no breakpoint: the track floor makes the
146
+ * grid reflow to whatever its **container** can hold, which is what a caller almost always
147
+ * means and is more nearly right than a viewport query — a grid inside a narrow panel
148
+ * should go one-column whatever the window is doing.
149
+ */
150
+ columns?: GridColumns;
151
+ /** Track floor for an `"auto"` grid (default `"sm"`); ignored at a fixed count. */
152
+ minColumn?: GridMinColumn;
153
+ template?: never;
154
+ }
155
+ | {
156
+ /**
157
+ * A named track template — the asymmetric composition, and the equal sets that collapse
158
+ * (ADR 0169 §2). See {@link GridTemplate}.
159
+ *
160
+ * A template is the layout of a page section: a chart beside the figures that explain
161
+ * it, a collection beside its summary. A fixed `columns` count keeps its tracks at every
162
+ * width, so four of them clip on a phone; a template gives up its tracks at the cutover
163
+ * instead, which is why it, and not a count, is how a section is laid out. Anything finer
164
+ * is nesting — a `Stack` in the narrow track holding two figures — never a span.
165
+ */
166
+ template: GridTemplate;
167
+ columns?: never;
168
+ minColumn?: never;
169
+ };
170
+
171
+ export type GridProps = GridBaseProps & GridTracks;
172
+
139
173
  /**
140
174
  * The two-dimensional layout primitive, and the one that lifts a real ceiling: with `Stack`
141
175
  * as the entire vocabulary, a two-column form could not be expressed at all, so a fifteen-field
@@ -158,17 +192,25 @@ export interface GridProps extends Omit<HTMLAttributes<HTMLElement>, "style"> {
158
192
  * wanting 17.5rem cannot have it, which is the same trade `gap` already makes.
159
193
  * - **There is no `span`,** and therefore no twelve-column option. A span system needs a child
160
194
  * component to carry it, and a `columns={12}` with no way to span is a grid of twelve narrow
161
- * cells rather than a layout system — worse than not offering it.
195
+ * cells rather than a layout system — worse than not offering it. Asymmetry is `template`
196
+ * instead (ADR 0169 §2): a closed set on the parent, so a row cannot go ragged — with spans,
197
+ * `2 + 2` in three columns leaves a hole that depends on how many children render — and
198
+ * every template collapses at the cutover by construction.
162
199
  */
163
200
  export function Grid({
164
201
  as: Component = "div",
165
202
  columns = "auto",
166
203
  minColumn = "sm",
204
+ template,
167
205
  gap = 4,
168
206
  padding,
169
207
  align = "stretch",
170
208
  ...rest
171
209
  }: GridProps) {
210
+ // A template decides the tracks on its own. The types already refuse a count beside it; this
211
+ // keeps an untyped caller from stamping both, where the count's rule would sit beside the
212
+ // template's and win or lose on source order.
213
+ const tracks = template === undefined ? columns : "auto";
172
214
  return (
173
215
  <Component
174
216
  {...rest}
@@ -176,8 +218,11 @@ export function Grid({
176
218
  // The defaults are the base rule, so their attributes match nothing and are left off —
177
219
  // the shape density and Button's `md` already use. String(columns) rather than the
178
220
  // number, because a `data-` attribute is text and `columns={2}` must produce "2".
179
- data-columns={columns === "auto" ? undefined : String(columns)}
180
- data-min-column={columns === "auto" && minColumn !== "sm" ? minColumn : undefined}
221
+ data-columns={tracks === "auto" ? undefined : String(tracks)}
222
+ data-min-column={
223
+ template === undefined && tracks === "auto" && minColumn !== "sm" ? minColumn : undefined
224
+ }
225
+ data-template={template}
181
226
  data-gap={String(gap)}
182
227
  data-padding={padding === undefined ? undefined : String(padding)}
183
228
  data-align={align === "stretch" ? undefined : align}
@@ -246,19 +291,31 @@ export interface DetailItem {
246
291
  }
247
292
 
248
293
  /** How a pair is arranged. */
249
- export type DetailListLayout = "inline" | "aligned" | "stacked";
294
+ export type DetailListLayout = "inline" | "aligned" | "stacked" | "grid";
250
295
 
251
296
  export interface DetailListProps extends Omit<HTMLAttributes<HTMLDListElement>, "style"> {
252
297
  /** The label/value pairs to render, in order. */
253
298
  items: readonly DetailItem[];
254
299
  /**
255
- * How each pair reads (default `"inline"` — `Label: value` on one line).
300
+ * How each pair reads (default `"stacked"` — the label above its value).
301
+ *
302
+ * `"inline"` runs `Label: value` on one line; `"aligned"` puts every label in a shared left
303
+ * column, so the values line up.
256
304
  *
257
- * `"aligned"` puts every label in a shared left column, so the values line up; `"stacked"`
258
- * puts the label above its value, which is what a narrow column or a long value wants.
305
+ * Stacked is the default because it is the one layout with no spacing to get wrong between a
306
+ * label and its value. Inline puts them a colon and a space apart, so how far a value sits
307
+ * from its label depended on the label's length and the value's, and a row of short labels
308
+ * beside long values -- the ordinary record card -- read as a ragged column of colons.
309
+ * Stacked gives every pair the same shape at any width, needs no label column, and is what a
310
+ * narrow column or a long value wanted anyway. It was `"inline"` until this release, kept for
311
+ * compatibility; an app that wants the run back says `layout="inline"`.
259
312
  *
260
- * The default is the old behaviour on purpose: this component is in the `standard` layout
261
- * contract's detail-page slot, so every governed detail screen already renders one.
313
+ * `"grid"` is the ruled grid (ADR 0169 §5): each pair a cell, its label above its value, the
314
+ * cells as many to a row as fit at 11rem and divided by hairline rules — a record's facts as a
315
+ * sheet you scan across, for the record whose facts are many and short. Unframed, like a
316
+ * group of figures, so it reads on a card or on the page alike; a cell that starts a row
317
+ * carries no rule before it, and the first row none above it. It takes `columns="auto"`
318
+ * unless told otherwise.
262
319
  */
263
320
  layout?: DetailListLayout;
264
321
  /**
@@ -351,8 +408,8 @@ export interface DetailListProps extends Omit<HTMLAttributes<HTMLDListElement>,
351
408
  */
352
409
  export function DetailList({
353
410
  items,
354
- layout = "inline",
355
- columns = 1,
411
+ layout = "stacked",
412
+ columns = layout === "grid" ? "auto" : 1,
356
413
  gap,
357
414
  ...rest
358
415
  }: DetailListProps) {
@@ -361,7 +418,10 @@ export function DetailList({
361
418
  <dl
362
419
  {...rest}
363
420
  data-terp="detail-list"
364
- // `inline` and one column are the base rule, so neither stamps an attribute.
421
+ // `inline` and one column are the base rule, so neither stamps an attribute. Stacked is
422
+ // the DEFAULT layout but not the base rule, so the default stamps "stacked": the sheet's
423
+ // stacked rules key on the attribute, and moving them onto the bare marker would have
424
+ // meant un-declaring them again under [data-layout="inline"] (the shape ADR 0094 avoids).
365
425
  data-layout={layout === "inline" ? undefined : layout}
366
426
  data-columns={columns === 1 ? undefined : String(columns)}
367
427
  // No default to compare against: the default row gap is the LAYOUT's, so an unset gap
@@ -1,7 +1,7 @@
1
1
  // @vitest-environment jsdom
2
2
  import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
3
3
  import type { ReactNode } from "react";
4
- import { Component } from "react";
4
+ import { Component, useEffect, useState } from "react";
5
5
  import { afterEach, describe, expect, it } from "vitest";
6
6
 
7
7
  // The lint-side source of the contract table (spec-as-data in @terpjs/eslint-boundaries);
@@ -10,6 +10,9 @@ import { afterEach, describe, expect, it } from "vitest";
10
10
  // @ts-expect-error — monorepo-relative untyped JS import, test-only
11
11
  import * as lintLayouts from "../../eslint-boundaries/src/layouts.js";
12
12
 
13
+ import { BarChart } from "./charts/BarChart";
14
+ import { TrendChart } from "./charts/TrendChart";
15
+ import { DashboardPage } from "./DashboardPage";
13
16
  import { DetailPage } from "./DetailPage";
14
17
  import { FormPage } from "./FormPage";
15
18
  import { SettingsPage } from "./SettingsPage";
@@ -20,9 +23,14 @@ import { HubCard, HubPage } from "./HubPage";
20
23
  import {
21
24
  LAYOUT_CONTRACTS,
22
25
  LayoutContractContext,
26
+ headlineViolationMessage,
23
27
  slotViolationMessage,
28
+ summaryViolationMessage,
24
29
  verifySlotChildren,
25
30
  } from "./layoutContract";
31
+ import { Stat, StatGroup } from "./Stat";
32
+ import { StatusHistory } from "./charts/StatusHistory";
33
+ import { Badge } from "./ui/Badge";
26
34
  import { OverviewPage } from "./OverviewPage";
27
35
 
28
36
  // The public surface, as source: the archetype-coverage check below derives its list from
@@ -104,6 +112,12 @@ describe("layout contract parity (docs/data can't drift)", () => {
104
112
  expect(slotViolationMessage("standard", "HubPage", "<div>")).toBe(
105
113
  lintLayouts.slotViolationMessage("standard", "HubPage", "<div>"),
106
114
  );
115
+ expect(summaryViolationMessage("standard", "<div>")).toBe(
116
+ lintLayouts.summaryViolationMessage("standard", "<div>"),
117
+ );
118
+ expect(headlineViolationMessage("standard", "2 figures marked headline")).toBe(
119
+ lintLayouts.headlineViolationMessage("standard", "2 figures marked headline"),
120
+ );
107
121
  });
108
122
  });
109
123
 
@@ -163,6 +177,7 @@ describe("every archetype the package exports is governed, or says it is not", (
163
177
  it("finds the archetype exports it is meant to check", () => {
164
178
  // Vacuity guard: a derivation that matched nothing would make every assertion below pass.
165
179
  expect(exported).toEqual([
180
+ "DashboardPage",
166
181
  "DetailPage",
167
182
  "FormPage",
168
183
  "HubPage",
@@ -302,6 +317,30 @@ describe("runtime slot enforcement", () => {
302
317
  });
303
318
  });
304
319
 
320
+ it("passes a governed DetailPage that steps through a sequence", async () => {
321
+ // The bar is a `nav` of the page's own, and the check reads every child of the article
322
+ // except its header as body — so a bar rendered inside the article would be judged a
323
+ // body child no slot table admits, and fail every governed page closed. It renders after
324
+ // the article instead, where the check never looks.
325
+ const { container } = underContract(
326
+ <DetailPage
327
+ title="Record 4"
328
+ parents={[{ label: "Records", to: "/records" }]}
329
+ sequence={{
330
+ label: "Records",
331
+ previous: { label: "Record 3", to: "/records/3" },
332
+ next: { label: "Record 5", to: "/records/5" },
333
+ position: { current: 4, total: 9 },
334
+ }}
335
+ >
336
+ <Card title="A section">the record</Card>
337
+ </DetailPage>,
338
+ );
339
+ await expectAccepted();
340
+ expect(container.querySelector('[data-terp="page"] [data-terp="page-sequence"]')).toBeNull();
341
+ expect(container.querySelector('[data-terp="page"] + [data-terp="page-sequence"]')).not.toBeNull();
342
+ });
343
+
305
344
  it("leaves the plain Page unconstrained (the contract's bespoke pressure valve)", async () => {
306
345
  underContract(
307
346
  <Page title="Bespoke">
@@ -392,8 +431,11 @@ describe("layout contract survives the roots the styling migration renames", ()
392
431
  Object.values(slot.components),
393
432
  ),
394
433
  );
434
+ // The dashboard's body (ADR 0169 §4) added the figure and chart families -- the run history
435
+ // among the charts -- and the timeline.
395
436
  expect([...named].sort()).toEqual([
396
437
  "alert",
438
+ "bar-chart",
397
439
  "card",
398
440
  "dataview",
399
441
  "detail-list",
@@ -406,11 +448,17 @@ describe("layout contract survives the roots the styling migration renames", ()
406
448
  "hubcard",
407
449
  "loading-state",
408
450
  "module-nav",
451
+ "proportion-bar",
409
452
  "resource-list",
410
453
  "splitpane",
411
454
  "stack",
455
+ "stat",
456
+ "stat-group",
457
+ "status-history",
412
458
  "tabs",
413
459
  "text",
460
+ "timeline",
461
+ "trend-chart",
414
462
  ]);
415
463
  });
416
464
 
@@ -584,3 +632,193 @@ describe("what 4b's widening does and does not admit", () => {
584
632
  }
585
633
  });
586
634
  });
635
+
636
+ describe("runtime enforcement of the page frame's two rules (ADR 0169 §4)", () => {
637
+ it("passes a summary of figures, a badge and a line of text", async () => {
638
+ underContract(
639
+ <DetailPage
640
+ title="Record 1"
641
+ parents={[{ label: "Records", to: "/records" }]}
642
+ summary={
643
+ <>
644
+ <StatGroup>
645
+ <Stat label="Open" value={3} headline />
646
+ <Stat label="Closed" value={9} />
647
+ </StatGroup>
648
+ <Badge tone="success">Active</Badge>
649
+ <Text>Synced four minutes ago.</Text>
650
+ </>
651
+ }
652
+ >
653
+ <Card title="A section">the record</Card>
654
+ </DetailPage>,
655
+ );
656
+ await expectAccepted();
657
+ });
658
+
659
+ it("refuses anything else in a summary, on an archetype, with the directive message", async () => {
660
+ // Mutation: drop the summary half of verifyPageFrame, and the card is accepted.
661
+ underContract(
662
+ <OverviewPage title="Records" summary={<Card title="Not a figure">x</Card>}>
663
+ <Stack>
664
+ <span>body</span>
665
+ </Stack>
666
+ </OverviewPage>,
667
+ );
668
+ await waitFor(() => {
669
+ expect(screen.getByTestId("refused").textContent).toBe(
670
+ summaryViolationMessage("standard", '<section data-terp="card">'),
671
+ );
672
+ });
673
+ });
674
+
675
+ it("governs the plain Page's summary too, whose body stays free", async () => {
676
+ // Mutation: run the frame check only under a slot owner again, and this is accepted.
677
+ underContract(
678
+ <Page title="Bespoke" summary={<div>loose</div>}>
679
+ <div>a bespoke body, which the contract leaves alone</div>
680
+ </Page>,
681
+ );
682
+ await waitFor(() => {
683
+ expect(screen.getByTestId("refused").textContent).toBe(
684
+ summaryViolationMessage("standard", "<div>"),
685
+ );
686
+ });
687
+ });
688
+
689
+ it("refuses a second headline anywhere on the page", async () => {
690
+ // One in the summary and one in the body is two on the page.
691
+ // Mutation: count only the summary's figures, and this is accepted.
692
+ underContract(
693
+ <Page title="Bespoke" summary={<Stat label="Revenue" value={1} headline />}>
694
+ <Grid template="1:1">
695
+ <Stat label="Margin" value={2} headline />
696
+ <Stat label="Costs" value={3} />
697
+ </Grid>
698
+ </Page>,
699
+ );
700
+ await waitFor(() => {
701
+ expect(screen.getByTestId("refused").textContent).toBe(
702
+ headlineViolationMessage("standard", "2 figures marked headline"),
703
+ );
704
+ });
705
+ });
706
+
707
+ it("passes a page with one headline", async () => {
708
+ underContract(
709
+ <Page title="Bespoke" summary={<Stat label="Revenue" value={1} headline />}>
710
+ <Grid template="1:1">
711
+ <Stat label="Margin" value={2} />
712
+ <Stat label="Costs" value={3} />
713
+ </Grid>
714
+ </Page>,
715
+ );
716
+ await expectAccepted();
717
+ });
718
+
719
+ it("checks nothing without a contract", async () => {
720
+ underContract(
721
+ <Page title="Bespoke" summary={<div>loose</div>}>
722
+ <Stat label="A" value={1} headline />
723
+ <Stat label="B" value={2} headline />
724
+ </Page>,
725
+ null,
726
+ );
727
+ await expectAccepted();
728
+ });
729
+ });
730
+
731
+ describe("a page counts the headlines that register with it (ADR 0169 §4)", () => {
732
+ function LateHeadline() {
733
+ // A section that shows its figure once its own data arrives: nothing renders the page again.
734
+ const [ready, setReady] = useState(false);
735
+ useEffect(() => {
736
+ const timer = setTimeout(() => setReady(true), 5);
737
+ return () => clearTimeout(timer);
738
+ }, []);
739
+ return ready ? <Stat label="Late" value={2} headline /> : null;
740
+ }
741
+
742
+ it("refuses a second headline that renders after the page did", async () => {
743
+ // A DOM count taken one macrotask after the page rendered found one headline and was never
744
+ // taken again. Mutation: count by DOM again, and the late figure ships.
745
+ underContract(
746
+ <Page title="Bespoke" summary={<Stat label="Revenue" value={1} headline />}>
747
+ <Stack>
748
+ <LateHeadline />
749
+ </Stack>
750
+ </Page>,
751
+ );
752
+ await waitFor(() => {
753
+ expect(screen.getByTestId("refused").textContent).toBe(
754
+ headlineViolationMessage("standard", "2 figures marked headline"),
755
+ );
756
+ });
757
+ });
758
+
759
+ it("counts a nested page's headline on its own, as the lint does", async () => {
760
+ // The outer page counted the inner page's figure as its own, and refused what the lint
761
+ // accepts. Mutation: provide one registry for both pages, and this is refused.
762
+ underContract(
763
+ <Page title="Outer">
764
+ <Page title="Inner" summary={<Stat label="Inner" value={1} headline />}>
765
+ <div>inner body</div>
766
+ </Page>
767
+ <Stat label="Outer" value={2} headline />
768
+ </Page>,
769
+ );
770
+ await expectAccepted();
771
+ });
772
+ });
773
+
774
+ describe("the summary band admits a status history (ADR 0169 §4)", () => {
775
+ it("passes a page whose summary holds the recent runs", async () => {
776
+ // Mutation: drop StatusHistory from the summary table, and this is refused.
777
+ underContract(
778
+ <Page
779
+ title="Customer master"
780
+ summary={
781
+ <StatusHistory
782
+ label="Runs"
783
+ runs={[{ label: "Mon", outcome: { label: "Succeeded", tone: "success" } }]}
784
+ />
785
+ }
786
+ >
787
+ <div>body</div>
788
+ </Page>,
789
+ );
790
+ await expectAccepted();
791
+ });
792
+ });
793
+
794
+ describe("the dashboard's body (ADR 0169 §4)", () => {
795
+ it("passes figures and charts in a templated section, and the collection they summarise", async () => {
796
+ underContract(
797
+ <DashboardPage title="Overview" summary={<StatGroup><Stat label="Runs" value={84} /></StatGroup>}>
798
+ <Grid template="2:1">
799
+ <TrendChart label="Runs per day" series={{ label: "Runs", points: [{ label: "Mon", value: 3 }] }} />
800
+ <BarChart label="By source" bars={[{ label: "Ledger", value: 9 }]} />
801
+ </Grid>
802
+ <TrendChart label="Rows" mark="columns" series={{ label: "Rows", points: [{ label: "Mon", value: 3 }] }} />
803
+ <Stat label="Failed runs" value={3} />
804
+ {/* Mutation: drop StatusHistory from the dashboard's table, and this is refused. */}
805
+ <StatusHistory label="Runs" runs={[{ label: "Mon", outcome: { label: "Succeeded", tone: "success" } }]} />
806
+ </DashboardPage>,
807
+ );
808
+ await expectAccepted();
809
+ });
810
+
811
+ it("refuses a body block the dashboard does not admit, with the directive message", async () => {
812
+ // Mutation: drop the DashboardPage slot table, and the dashboard's body is unconstrained.
813
+ underContract(
814
+ <DashboardPage title="Overview">
815
+ <div>hand-rolled tiles</div>
816
+ </DashboardPage>,
817
+ );
818
+ await waitFor(() => {
819
+ expect(screen.getByTestId("refused").textContent).toBe(
820
+ slotViolationMessage("standard", "DashboardPage", "<div>"),
821
+ );
822
+ });
823
+ });
824
+ });
@@ -24,10 +24,14 @@ export interface LayoutSlotSpec {
24
24
  readonly components: Readonly<Record<string, string>>;
25
25
  }
26
26
 
27
- /** One named layout contract: a description and its per-archetype slot specs. */
27
+ /** One named layout contract: a description, its per-archetype slot specs and its frame rules. */
28
28
  export interface LayoutContractSpec {
29
29
  readonly description: string;
30
30
  readonly slots: Readonly<Record<string, LayoutSlotSpec>>;
31
+ /** What a page's summary band admits (ADR 0169 §4), on every page, the plain one included. */
32
+ readonly summary: LayoutSlotSpec;
33
+ /** The components whose `headline` marks the page's one headline figure (ADR 0169 §4). */
34
+ readonly headline: LayoutSlotSpec;
31
35
  }
32
36
 
33
37
  /** Every layout contract, keyed by id (mirror of the eslint-boundaries source table). */
@@ -43,9 +47,15 @@ export const LAYOUT_CONTRACTS: Readonly<Record<string, LayoutContractSpec>> = {
43
47
  "beside those: a form body is a container (Stack) with optional Grid / Card " +
44
48
  "sections and no Field at the top level, so the body is always a container rather " +
45
49
  "than a loose run of controls; a settings body is Card sections and holds no " +
46
- "collection; and a split " +
47
- "body is two SplitPanes and nothing else. A screen that needs no contract " +
48
- "composes the plain Page, which this contract deliberately leaves unconstrained.",
50
+ "collection; a split " +
51
+ "body is two SplitPanes and nothing else; and a dashboard body is its figures and " +
52
+ "charts in Grid sections, the collection they summarise and the framework states. " +
53
+ "A screen that needs no contract " +
54
+ "composes the plain Page, whose body this contract deliberately leaves unconstrained. " +
55
+ "Every page, the plain one included, keeps two rules of its frame: its summary band " +
56
+ "holds the page's own figures (Stat / StatGroup / StatusHistory / Badge / Text), and " +
57
+ "at most one " +
58
+ "figure on the page is its headline.",
49
59
  slots: {
50
60
  HubPage: {
51
61
  components: { HubCard: "hubcard" },
@@ -96,6 +106,28 @@ export const LAYOUT_CONTRACTS: Readonly<Record<string, LayoutContractSpec>> = {
96
106
  SplitPage: {
97
107
  components: { SplitPane: "splitpane" },
98
108
  },
109
+ DashboardPage: {
110
+ components: {
111
+ Grid: "grid",
112
+ Stack: "stack",
113
+ Card: "card",
114
+ DataView: "dataview",
115
+ Stat: "stat",
116
+ StatGroup: "stat-group",
117
+ TrendChart: "trend-chart",
118
+ BarChart: "bar-chart",
119
+ ProportionBar: "proportion-bar",
120
+ StatusHistory: "status-history",
121
+ Timeline: "timeline",
122
+ Divider: "divider",
123
+ Text: "text",
124
+ EmptyState: "empty-state",
125
+ ErrorState: "error-state",
126
+ LoadingState: "loading-state",
127
+ Alert: "alert",
128
+ ConfirmDialog: "dialog",
129
+ },
130
+ },
99
131
  DetailPage: {
100
132
  components: {
101
133
  DetailList: "detail-list",
@@ -116,6 +148,23 @@ export const LAYOUT_CONTRACTS: Readonly<Record<string, LayoutContractSpec>> = {
116
148
  },
117
149
  },
118
150
  },
151
+ // The band a page renders under its title band when it is given a summary (ADR 0169 §4):
152
+ // the page's own figures. Governed on every page, the plain Page included, because the
153
+ // band belongs to the frame and not to a body.
154
+ summary: {
155
+ components: {
156
+ Stat: "stat",
157
+ StatGroup: "stat-group",
158
+ StatusHistory: "status-history",
159
+ Badge: "badge",
160
+ Text: "text",
161
+ },
162
+ },
163
+ // The components whose `headline` prop (rendered as `data-headline`) marks the page's
164
+ // headline figure, of which a page carries at most one (ADR 0169 §4).
165
+ headline: {
166
+ components: { Stat: "stat" },
167
+ },
119
168
  },
120
169
  };
121
170
 
@@ -140,9 +189,56 @@ export function slotViolationMessage(
140
189
  );
141
190
  }
142
191
 
192
+ /**
193
+ * The directive message for a page's summary band (ADR 0169 §4): what it admits, what was
194
+ * found, and the fix. (Byte-identical to the eslint-boundaries builder; parity-tested.)
195
+ */
196
+ export function summaryViolationMessage(contractId: string, found: string): string {
197
+ const allowed = Object.keys(LAYOUT_CONTRACTS[contractId]!.summary.components);
198
+ return (
199
+ `Layout contract "${contractId}": a page's summary band accepts only ` +
200
+ `${allowed.join(" / ")}; found ${found}. The band carries the page's own figures, so ` +
201
+ "move anything else into the body (recipe: terp guide layouts), " +
202
+ "or opt out on this line with a justified // terp-allow-layout-contract: <reason> " +
203
+ "marker (counted by the escape-hatch budget)."
204
+ );
205
+ }
206
+
207
+ /**
208
+ * The directive message for a page that carries more than one headline figure (ADR 0169 §4).
209
+ * (Byte-identical to the eslint-boundaries builder; parity-tested.)
210
+ */
211
+ export function headlineViolationMessage(contractId: string, found: string): string {
212
+ return (
213
+ `Layout contract "${contractId}": a page carries at most one headline figure; found ` +
214
+ `${found}. Keep headline on the one figure the page is about and render the others as ` +
215
+ "ordinary figures (recipe: terp guide layouts), " +
216
+ "or opt out on this line with a justified // terp-allow-layout-contract: <reason> " +
217
+ "marker (counted by the escape-hatch budget)."
218
+ );
219
+ }
220
+
143
221
  /** The active contract id for the current routed view, or null (no contract = no checks). */
144
222
  export const LayoutContractContext = createContext<string | null>(null);
145
223
 
224
+ /** A page's count of the figures marked headline, which each such figure joins. */
225
+ export interface HeadlineRegistry {
226
+ /** Count one more headline; the function it returns counts that one out again. */
227
+ register(): () => void;
228
+ }
229
+
230
+ /**
231
+ * The nearest page's headline registry (ADR 0169 §4), or null outside a page.
232
+ *
233
+ * A registry rather than a DOM count, and the difference is when it looks. A count taken one
234
+ * macrotask after the page renders missed a figure that rendered later -- a section that loads
235
+ * its own data and then shows its headline -- because nothing renders the page again, and a
236
+ * `querySelectorAll` over the article counted a nested page's figures as the outer page's,
237
+ * which the lint does not. A figure that registers is counted whenever it mounts, by the page
238
+ * it is nearest to, and only by that one.
239
+ */
240
+ export const HeadlineContext = createContext<HeadlineRegistry | null>(null);
241
+
146
242
  /** Read the active layout contract id (null outside an opted-in app). */
147
243
  export function useLayoutContract(): string | null {
148
244
  return useContext(LayoutContractContext);
@@ -176,12 +272,42 @@ export function verifySlotChildren(
176
272
  if (slot === undefined) {
177
273
  return null;
178
274
  }
275
+ return firstStranger(slot, children, (found) => slotViolationMessage(contractId, slotOwner, found));
276
+ }
277
+
278
+ /** The first child whose marker the slot does not admit, phrased by `describe`; or null. */
279
+ function firstStranger(
280
+ slot: LayoutSlotSpec,
281
+ children: readonly Element[],
282
+ describe: (found: string) => string,
283
+ ): string | null {
179
284
  const allowed = new Set(Object.values(slot.components));
180
285
  for (const child of children) {
181
286
  const marker = child.getAttribute("data-terp");
182
287
  if (marker === null || !allowed.has(marker)) {
183
- return slotViolationMessage(contractId, slotOwner, describeElement(child));
288
+ return describe(describeElement(child));
184
289
  }
185
290
  }
186
291
  return null;
187
292
  }
293
+
294
+ /**
295
+ * Verify a page's summary band against the contract (ADR 0169 §4): its children are the
296
+ * figures it admits. Returns the directive message, or null when the band conforms or there is
297
+ * none. The frame's other rule, one headline per page, is counted by registration instead (see
298
+ * {@link HeadlineContext}).
299
+ */
300
+ export function verifyPageSummary(contractId: string, page: Element): string | null {
301
+ const contract = LAYOUT_CONTRACTS[contractId];
302
+ if (contract === undefined) {
303
+ return null;
304
+ }
305
+ const summary = [...page.children].find(
306
+ (child) => child.getAttribute("data-terp") === "page-summary",
307
+ );
308
+ return summary === undefined
309
+ ? null
310
+ : firstStranger(contract.summary, [...summary.children], (found) =>
311
+ summaryViolationMessage(contractId, found),
312
+ );
313
+ }