Skip to content

Commit d79b631

Browse files
test(contracts): cover header footer inheritance helper
1 parent 9a508c5 commit d79b631

2 files changed

Lines changed: 99 additions & 11 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, expect, it } from 'vitest';
2+
import {
3+
resolveInheritedHeaderFooterRef,
4+
resolveInheritedHeaderFooterRefWithType,
5+
} from './header-footer-inheritance.js';
6+
7+
describe('header/footer inheritance', () => {
8+
it('uses legacy refs when section maps are empty', () => {
9+
const ref = resolveInheritedHeaderFooterRef({
10+
identifier: {
11+
headerIds: { default: 'legacy-default' },
12+
sectionHeaderIds: new Map(),
13+
},
14+
sectionIndex: 0,
15+
kind: 'header',
16+
variantType: 'default',
17+
});
18+
19+
expect(ref).toBe('legacy-default');
20+
});
21+
22+
it('returns null for section zero when no page, section, or legacy refs exist', () => {
23+
const ref = resolveInheritedHeaderFooterRef({
24+
identifier: { sectionHeaderIds: new Map() },
25+
sectionIndex: 0,
26+
kind: 'header',
27+
variantType: 'default',
28+
});
29+
30+
expect(ref).toBeNull();
31+
});
32+
33+
it('walks back past intermediate sections with no entry', () => {
34+
const ref = resolveInheritedHeaderFooterRef({
35+
identifier: {
36+
sectionHeaderIds: new Map([[0, { first: 'section-0-first' }]]),
37+
},
38+
sectionIndex: 3,
39+
kind: 'header',
40+
variantType: 'first',
41+
});
42+
43+
expect(ref).toBe('section-0-first');
44+
});
45+
46+
it('prefers page refs over section refs', () => {
47+
const resolved = resolveInheritedHeaderFooterRefWithType({
48+
identifier: {
49+
sectionFooterIds: new Map([[0, { default: 'section-default' }]]),
50+
},
51+
sectionIndex: 0,
52+
kind: 'footer',
53+
variantType: 'default',
54+
pageRefs: { default: 'page-default' },
55+
});
56+
57+
expect(resolved).toEqual({ ref: 'page-default', variantType: 'default' });
58+
});
59+
60+
it('uses default refs for odd pages and reports the effective variant', () => {
61+
const resolved = resolveInheritedHeaderFooterRefWithType({
62+
identifier: {
63+
headerIds: { default: 'legacy-default' },
64+
},
65+
sectionIndex: 0,
66+
kind: 'header',
67+
variantType: 'odd',
68+
});
69+
70+
expect(resolved).toEqual({ ref: 'legacy-default', variantType: 'default' });
71+
});
72+
73+
it('does not fall back from first to default', () => {
74+
const ref = resolveInheritedHeaderFooterRef({
75+
identifier: {
76+
headerIds: { default: 'legacy-default' },
77+
},
78+
sectionIndex: 0,
79+
kind: 'header',
80+
variantType: 'first',
81+
});
82+
83+
expect(ref).toBeNull();
84+
});
85+
86+
it('does not fall back from even to default', () => {
87+
const ref = resolveInheritedHeaderFooterRef({
88+
identifier: {
89+
headerIds: { default: 'legacy-default' },
90+
},
91+
sectionIndex: 0,
92+
kind: 'header',
93+
variantType: 'even',
94+
});
95+
96+
expect(ref).toBeNull();
97+
});
98+
});

packages/layout-engine/contracts/src/header-footer-inheritance.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type HeaderFooterType = 'default' | 'first' | 'even' | 'odd';
1+
import type { HeaderFooterType } from './index.js';
22

33
export type HeaderFooterRefMap = Partial<Record<HeaderFooterType, string | null | undefined>>;
44

@@ -51,17 +51,7 @@ export function resolveInheritedHeaderFooterRefWithType({
5151
const fromSection = resolveVariantRef(sectionIds, variantType);
5252
if (fromSection) return fromSection;
5353

54-
let hasPriorSectionRefs = false;
5554
if (sectionMap) {
56-
for (const index of sectionMap.keys()) {
57-
if (index < sectionIndex) {
58-
hasPriorSectionRefs = true;
59-
break;
60-
}
61-
}
62-
}
63-
const hasSectionAwareRefs = sectionMap != null && (sectionMap.has(sectionIndex) || hasPriorSectionRefs);
64-
if (hasSectionAwareRefs) {
6555
for (let index = sectionIndex - 1; index >= 0; index -= 1) {
6656
const inherited = resolveVariantRef(sectionMap.get(index), variantType);
6757
if (inherited) return inherited;

0 commit comments

Comments
 (0)