Skip to content

Commit 20ffc04

Browse files
committed
test(painter): lock ResolvedLayout-only boundary (SD-2836)
1 parent 1c869c1 commit 20ffc04

4 files changed

Lines changed: 94 additions & 3 deletions

File tree

packages/layout-engine/AGENTS.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ ProseMirror Doc → pm-adapter → FlowBlock[] → layout-engine → Layout[]
2222

2323
## Key Insight: DomPainter is "Dumb"
2424

25-
DomPainter receives pre-computed `Layout` with positioned fragments and renders them.
26-
It does NOT do layout logic - that's in `layout-engine/`.
25+
DomPainter receives a single paint-ready input — `ResolvedLayout` — with
26+
positioned fragments, pre-resolved styles, and `fragment` back-pointers on
27+
every `ResolvedPaintItem` — and renders the result to DOM. It does NOT do
28+
layout logic, measurement, or PM-adapter conversion (that's upstream in
29+
`layout-engine/` / `layout-resolved/` / `pm-adapter/`).
30+
31+
The painter has zero runtime imports from `@superdoc/pm-adapter`,
32+
`@superdoc/layout-bridge`, or `@superdoc/layout-resolved`. Architecture
33+
boundary tests in `tests/src/architecture-boundaries.test.ts` (Guard D)
34+
enforce this.
2735

2836
## Common Tasks
2937

packages/layout-engine/painters/dom/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"@superdoc/contracts": "workspace:*",
2222
"@superdoc/dom-contract": "workspace:*",
2323
"@superdoc/font-utils": "workspace:*",
24-
"@superdoc/layout-resolved": "workspace:*",
2524
"@superdoc/preset-geometry": "workspace:*",
2625
"@superdoc/url-validation": "workspace:*"
2726
},
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Compile-time + runtime contract lockdown for the painter's public surface.
3+
*
4+
* These assertions fail when someone reintroduces a legacy field on
5+
* `DomPainterInput`, adds a method to `DomPainterHandle`, or makes
6+
* `PageDecorationPayload.items` optional. The boundary tests in
7+
* `tests/src/architecture-boundaries.test.ts` cover the import side; this
8+
* file covers the type-shape side.
9+
*/
10+
import { describe, expectTypeOf, it } from 'vitest';
11+
import type { ResolvedLayout, ResolvedPaintItem } from '@superdoc/contracts';
12+
import type { DomPainterHandle, DomPainterInput, PageDecorationPayload } from './index.js';
13+
14+
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
15+
type AssertTrue<T extends true> = T;
16+
17+
describe('DomPainter public contract shape', () => {
18+
it('DomPainterInput is exactly { resolvedLayout: ResolvedLayout }', () => {
19+
type _Check = AssertTrue<Equal<DomPainterInput, { resolvedLayout: ResolvedLayout }>>;
20+
expectTypeOf<DomPainterInput>().toEqualTypeOf<{ resolvedLayout: ResolvedLayout }>();
21+
});
22+
23+
it('DomPainterHandle exposes only the painter-owned methods', () => {
24+
type ExpectedKeys =
25+
| 'paint'
26+
| 'setProviders'
27+
| 'setVirtualizationPins'
28+
| 'getMountedPageIndices'
29+
| 'onScroll'
30+
| 'setZoom'
31+
| 'setScrollContainer';
32+
type _Check = AssertTrue<Equal<keyof DomPainterHandle, ExpectedKeys>>;
33+
expectTypeOf<keyof DomPainterHandle>().toEqualTypeOf<ExpectedKeys>();
34+
});
35+
36+
it('PageDecorationPayload.items is required (synthesis path is gone)', () => {
37+
type ItemsType = PageDecorationPayload['items'];
38+
type _Check = AssertTrue<Equal<ItemsType, ResolvedPaintItem[]>>;
39+
expectTypeOf<ItemsType>().toEqualTypeOf<ResolvedPaintItem[]>();
40+
});
41+
});

packages/layout-engine/tests/src/architecture-boundaries.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,47 @@ describe('architecture boundaries', () => {
187187
expectNoViolations(findRelativeImportViolations(srcDir, /from\s+['"].*layout-engine\//));
188188
});
189189
});
190+
191+
describe('Guard D: painter-dom is a dumb final renderer with no upstream dependencies', () => {
192+
it('painter-dom runtime src does not import @superdoc/pm-adapter', () => {
193+
const srcDir = path.join(LAYOUT_ENGINE_ROOT, 'painters/dom/src');
194+
expectNoViolations(findImportViolations(srcDir, '@superdoc/pm-adapter'));
195+
});
196+
197+
it('painter-dom runtime src does not import @superdoc/layout-bridge', () => {
198+
const srcDir = path.join(LAYOUT_ENGINE_ROOT, 'painters/dom/src');
199+
expectNoViolations(findImportViolations(srcDir, '@superdoc/layout-bridge'));
200+
});
201+
202+
it('painter-dom runtime src does not import @superdoc/layout-resolved (test-only utility)', () => {
203+
const srcDir = path.join(LAYOUT_ENGINE_ROOT, 'painters/dom/src');
204+
// _test-utils.ts is test-only and excluded from runtime collection. The
205+
// architecture-boundary check passes when no runtime file imports
206+
// layout-resolved.
207+
const files = collectRuntimeSources(srcDir).filter((f) => !f.endsWith('_test-utils.ts'));
208+
const violations: { file: string; line: string }[] = [];
209+
const pattern = new RegExp(`['"]@superdoc/layout-resolved(?:[/'"]|$)`);
210+
for (const file of files) {
211+
const raw = fs.readFileSync(file, 'utf-8');
212+
const processed = preprocessSource(raw);
213+
const lines = processed.split('\n');
214+
for (const ln of lines) {
215+
if (pattern.test(ln)) {
216+
violations.push({ file: path.relative(LAYOUT_ENGINE_ROOT, file), line: ln.trim() });
217+
}
218+
}
219+
}
220+
expectNoViolations(violations);
221+
});
222+
223+
it('painter-dom runtime src does not import relative pm-adapter paths', () => {
224+
const srcDir = path.join(LAYOUT_ENGINE_ROOT, 'painters/dom/src');
225+
expectNoViolations(findRelativeImportViolations(srcDir, /from\s+['"].*pm-adapter\//));
226+
});
227+
228+
it('painter-dom runtime src does not import relative layout-bridge paths', () => {
229+
const srcDir = path.join(LAYOUT_ENGINE_ROOT, 'painters/dom/src');
230+
expectNoViolations(findRelativeImportViolations(srcDir, /from\s+['"].*layout-bridge\//));
231+
});
232+
});
190233
});

0 commit comments

Comments
 (0)