@lemonriver/core 0.5.0 → 0.6.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 (57) hide show
  1. package/dist/{build-CS1goZxt.js → build-RlcGG-Ve.js} +1 -1
  2. package/dist/cli/bin.js +3 -3
  3. package/dist/{config-BpbcCbUv.js → config-BJT87wE9.js} +1155 -19
  4. package/dist/{dev-BpYnfgLL.js → dev-DR-j_xda.js} +1 -1
  5. package/dist/{preview-HSzPPkSs.js → preview-BTAhTb5B.js} +1 -1
  6. package/dist/vite/index.js +1 -1
  7. package/package.json +1 -1
  8. package/src/app/lemonriver/board/blocks/classify.test.ts +5 -0
  9. package/src/app/lemonriver/board/blocks/classify.ts +29 -2
  10. package/src/app/lemonriver/board/blocks/snapshot.ts +34 -4
  11. package/src/app/lemonriver/board/board-page.tsx +15 -0
  12. package/src/app/lemonriver/board/model/agent-tasks.ts +33 -1
  13. package/src/app/lemonriver/board/model/self-check.test.ts +329 -0
  14. package/src/app/lemonriver/board/model/self-check.ts +489 -0
  15. package/src/app/lemonriver/board/page-node.tsx +23 -1
  16. package/src/app/lemonriver/board/self-check-jump.ts +23 -0
  17. package/src/app/lemonriver/board/self-check-panel.tsx +272 -0
  18. package/src/app/lemonriver/board/self-check-runner.tsx +150 -0
  19. package/src/app/lemonriver/board/use-self-check.ts +117 -0
  20. package/src/app/lemonriver/chrome/filter-pill.tsx +30 -0
  21. package/src/app/lemonriver/chrome/sidebar-extras.tsx +12 -1
  22. package/src/app/lemonriver/features.test.ts +5 -0
  23. package/src/app/lemonriver/features.ts +2 -1
  24. package/src/app/lemonriver/home/deck-card.tsx +10 -0
  25. package/src/app/lemonriver/home/home-page.tsx +55 -6
  26. package/src/app/lemonriver/home/use-deck-times.ts +4 -1
  27. package/src/app/lemonriver/outline/build-team.tsx +42 -0
  28. package/src/app/lemonriver/outline/describe-page.tsx +128 -27
  29. package/src/app/lemonriver/outline/draft.ts +38 -0
  30. package/src/app/lemonriver/outline/model.test.ts +79 -0
  31. package/src/app/lemonriver/outline/model.ts +80 -7
  32. package/src/app/lemonriver/outline/outline-page.tsx +56 -6
  33. package/src/app/lemonriver/outline/pick-style.tsx +282 -0
  34. package/src/app/lemonriver/routes.test.tsx +17 -5
  35. package/src/app/lemonriver/routes.tsx +28 -0
  36. package/src/app/lemonriver/templates/client.ts +60 -0
  37. package/src/app/lemonriver/templates/demo-page.tsx +79 -0
  38. package/src/app/lemonriver/templates/knobs.test.ts +62 -0
  39. package/src/app/lemonriver/templates/knobs.ts +62 -0
  40. package/src/app/lemonriver/templates/library-page.tsx +292 -0
  41. package/src/app/lemonriver/templates/model.test.ts +175 -0
  42. package/src/app/lemonriver/templates/model.ts +339 -0
  43. package/src/app/lemonriver/templates/preview-content.test.ts +117 -0
  44. package/src/app/lemonriver/templates/preview-content.ts +149 -0
  45. package/src/app/lemonriver/templates/recommend.test.ts +71 -0
  46. package/src/app/lemonriver/templates/recommend.ts +72 -0
  47. package/src/app/lemonriver/templates/use-library.ts +58 -0
  48. package/src/app/lemonriver/templates/virtual.d.ts +28 -0
  49. package/src/app/lemonriver/text/en.ts +57 -0
  50. package/src/app/lemonriver/text/es.ts +57 -0
  51. package/src/app/lemonriver/text/ja.ts +57 -0
  52. package/src/app/lemonriver/text/types.ts +73 -0
  53. package/src/app/lemonriver/text/zh-cn.ts +57 -0
  54. package/src/app/lemonriver/text/zh-tw.ts +57 -0
  55. package/src/app/lemonriver/theme/pages.test.ts +4 -0
  56. package/src/app/lemonriver/theme/pages.ts +6 -1
  57. package/src/app/lemonriver/update/prompt.ts +1 -1
@@ -0,0 +1,175 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ applyKnobs,
4
+ designSnippet,
5
+ knobFlags,
6
+ sanitizeAppliedTemplate,
7
+ sanitizeKnobValues,
8
+ sanitizeTemplate,
9
+ } from './model.ts';
10
+
11
+ const design = {
12
+ palette: { bg: '#f4f1ea', text: '#1a1a1a', accent: '#d9480f' },
13
+ fonts: { display: '"Songti SC", serif', body: '"PingFang SC", sans-serif' },
14
+ typeScale: { hero: 420, body: 30 },
15
+ radius: 0,
16
+ };
17
+
18
+ const knob = (label: string) => ({
19
+ left: { label: `${label}左`, design: { palette: { accent: '#2244aa' } } },
20
+ right: { label: `${label}右`, flags: { motion: 'lively' } },
21
+ });
22
+
23
+ const raw = () => ({
24
+ id: 'board-report',
25
+ name: '董事会报告',
26
+ occasions: ['report', 'review'],
27
+ audience: '董事会成员与高管',
28
+ object: '正式董事会报告',
29
+ reason: '数字和审批一目了然',
30
+ accent: '最新值与计划的差额',
31
+ design,
32
+ knobs: { formal: knob('正式'), bright: knob('亮'), motion: knob('动效') },
33
+ skeletons: ['cover', 'overview', 'ask', 'end'],
34
+ fonts: [],
35
+ source: 'northfold',
36
+ });
37
+
38
+ describe('sanitizeTemplate', () => {
39
+ it('accepts a complete template and trims its text', () => {
40
+ const input = raw();
41
+ input.name = ' 董事会报告 ';
42
+ const read = sanitizeTemplate(input, 'board-report');
43
+ expect(read.ok).toBe(true);
44
+ if (!read.ok) return;
45
+ expect(read.template.name).toBe('董事会报告');
46
+ expect(read.template.occasions).toEqual(['report', 'review']);
47
+ expect(read.template.source).toBe('northfold');
48
+ });
49
+
50
+ it('drops a template without occasions', () => {
51
+ const input: Record<string, unknown> = raw();
52
+ delete input.occasions;
53
+ const read = sanitizeTemplate(input, 'board-report');
54
+ expect(read).toEqual({ ok: false, reason: 'missing occasions' });
55
+ });
56
+
57
+ it('drops an empty occasions list', () => {
58
+ const read = sanitizeTemplate({ ...raw(), occasions: [] }, 'board-report');
59
+ expect(read.ok).toBe(false);
60
+ });
61
+
62
+ it('drops an occasion outside the seven', () => {
63
+ const read = sanitizeTemplate({ ...raw(), occasions: ['pitch', 'wedding'] }, 'board-report');
64
+ expect(read).toEqual({ ok: false, reason: 'unknown occasion "wedding"' });
65
+ });
66
+
67
+ it('drops an unknown skeleton', () => {
68
+ const read = sanitizeTemplate({ ...raw(), skeletons: ['cover', 'carousel'] }, 'board-report');
69
+ expect(read).toEqual({ ok: false, reason: 'unknown skeleton "carousel"' });
70
+ });
71
+
72
+ it('drops an id that disagrees with its folder', () => {
73
+ const read = sanitizeTemplate(raw(), 'other-folder');
74
+ expect(read.ok).toBe(false);
75
+ if (read.ok) return;
76
+ expect(read.reason).toContain('does not match the folder');
77
+ });
78
+
79
+ it('drops an id with path characters', () => {
80
+ const read = sanitizeTemplate({ ...raw(), id: '../escape' }, '../escape');
81
+ expect(read.ok).toBe(false);
82
+ });
83
+
84
+ it('drops a design missing an accent colour', () => {
85
+ const read = sanitizeTemplate(
86
+ { ...raw(), design: { ...design, palette: { bg: '#fff', text: '#000' } } },
87
+ 'board-report',
88
+ );
89
+ expect(read).toEqual({ ok: false, reason: 'missing or malformed design' });
90
+ });
91
+
92
+ it('drops knobs missing one of the three', () => {
93
+ const knobs = raw().knobs as Record<string, unknown>;
94
+ const read = sanitizeTemplate(
95
+ { ...raw(), knobs: { formal: knobs.formal, bright: knobs.bright } },
96
+ 'board-report',
97
+ );
98
+ expect(read).toEqual({ ok: false, reason: 'missing or malformed knobs' });
99
+ });
100
+
101
+ it('treats an absent source as null', () => {
102
+ const input: Record<string, unknown> = raw();
103
+ delete input.source;
104
+ const read = sanitizeTemplate(input, 'board-report');
105
+ expect(read.ok).toBe(true);
106
+ if (read.ok) expect(read.template.source).toBeNull();
107
+ });
108
+
109
+ it('reads font licence pairs', () => {
110
+ const read = sanitizeTemplate(
111
+ {
112
+ ...raw(),
113
+ fonts: [{ file: 'fonts/Pixel.ttf', license: 'fonts/OFL.txt' }],
114
+ },
115
+ 'board-report',
116
+ );
117
+ expect(read.ok).toBe(true);
118
+ if (read.ok) expect(read.template.fonts).toHaveLength(1);
119
+ });
120
+
121
+ it('rejects a font entry without a licence', () => {
122
+ const read = sanitizeTemplate(
123
+ { ...raw(), fonts: [{ file: 'fonts/Pixel.ttf' }] },
124
+ 'board-report',
125
+ );
126
+ expect(read.ok).toBe(false);
127
+ });
128
+ });
129
+
130
+ describe('knobs', () => {
131
+ const read = sanitizeTemplate(raw(), 'board-report');
132
+ if (!read.ok) throw new Error('fixture template is invalid');
133
+
134
+ it('keeps the base design in the middle', () => {
135
+ expect(applyKnobs(read.template.design, read.template.knobs, { formal: 0.5 })).toEqual(design);
136
+ });
137
+
138
+ it('leans left below a third', () => {
139
+ const out = applyKnobs(read.template.design, read.template.knobs, { formal: 0.1 });
140
+ expect(out.palette.accent).toBe('#2244aa');
141
+ expect(out.palette.bg).toBe(design.palette.bg);
142
+ });
143
+
144
+ it('leans right above two thirds and reports its flags', () => {
145
+ const out = applyKnobs(read.template.design, read.template.knobs, { motion: 0.9 });
146
+ expect(out.palette.accent).toBe(design.palette.accent);
147
+ expect(knobFlags(read.template.knobs, { motion: 0.9 })).toEqual({ motion: 'lively' });
148
+ });
149
+
150
+ it('keeps knob values inside [0, 1]', () => {
151
+ expect(sanitizeKnobValues({ formal: 2, bright: -0.2, motion: 0.4 })).toEqual({
152
+ motion: 0.4,
153
+ });
154
+ });
155
+ });
156
+
157
+ describe('applied record', () => {
158
+ it('reads back what the board stores', () => {
159
+ expect(
160
+ sanitizeAppliedTemplate({ id: 'board-report', knobs: { formal: 0.8 }, appliedAt: 12 }),
161
+ ).toEqual({ id: 'board-report', knobs: { formal: 0.8 }, appliedAt: 12 });
162
+ });
163
+
164
+ it('drops a record without a template id', () => {
165
+ expect(sanitizeAppliedTemplate({ knobs: {}, appliedAt: 12 })).toBeNull();
166
+ });
167
+ });
168
+
169
+ describe('designSnippet', () => {
170
+ it('writes a design constant the deck can paste', () => {
171
+ const snippet = designSnippet(design);
172
+ expect(snippet.startsWith('export const design: DesignSystem = {')).toBe(true);
173
+ expect(snippet).toContain('"accent": "#d9480f"');
174
+ });
175
+ });
@@ -0,0 +1,339 @@
1
+ import type { DesignSystem } from '../../lib/design.ts';
2
+
3
+ export const OCCASIONS = [
4
+ { id: 'pitch', name: '融资路演' },
5
+ { id: 'report', name: '经营汇报' },
6
+ { id: 'launch', name: '产品发布' },
7
+ { id: 'review', name: '专业评审' },
8
+ { id: 'event', name: '活动招商与品牌' },
9
+ { id: 'training', name: '培训课程' },
10
+ { id: 'retro', name: '复盘分享' },
11
+ ] as const;
12
+
13
+ export type OccasionId = (typeof OCCASIONS)[number]['id'];
14
+
15
+ const OCCASION_IDS = new Set<string>(OCCASIONS.map((occasion) => occasion.id));
16
+
17
+ export const SKELETONS = [
18
+ { id: 'cover', name: '封面', component: 'CoverSkeleton' },
19
+ { id: 'overview', name: '一页总览', component: 'OverviewSkeleton' },
20
+ { id: 'bignum', name: '超大单数字', component: 'BignumSkeleton' },
21
+ { id: 'figure', name: '主图加右栏', component: 'FigureSkeleton' },
22
+ { id: 'compare', name: '前后对比', component: 'CompareSkeleton' },
23
+ { id: 'cards', name: '三卡或四卡并列', component: 'CardsSkeleton' },
24
+ { id: 'process', name: '原理或流程', component: 'ProcessSkeleton' },
25
+ { id: 'timeline', name: '时间线', component: 'TimelineSkeleton' },
26
+ { id: 'risk', name: '风险', component: 'RiskSkeleton' },
27
+ { id: 'ask', name: '请求', component: 'AskSkeleton' },
28
+ { id: 'section', name: '章节页', component: 'SectionSkeleton' },
29
+ { id: 'quote', name: '引用', component: 'QuoteSkeleton' },
30
+ { id: 'end', name: '结尾', component: 'EndSkeleton' },
31
+ ] as const;
32
+
33
+ export type SkeletonId = (typeof SKELETONS)[number]['id'];
34
+
35
+ const SKELETON_IDS = new Set<string>(SKELETONS.map((skeleton) => skeleton.id));
36
+
37
+ /** A template may lower the small-type floor, never lift it. */
38
+ export const MIN_TYPE_FLOOR = { min: 16, max: 20 } as const;
39
+
40
+ export type TemplateKnobValues = { formal?: number; bright?: number; motion?: number };
41
+
42
+ export type TemplateKnobEnd = {
43
+ label: string;
44
+ design?: DesignOverrides;
45
+ flags?: Record<string, string | number | boolean>;
46
+ };
47
+
48
+ export type TemplateKnob = { left: TemplateKnobEnd; right: TemplateKnobEnd };
49
+
50
+ export type TemplateKnobs = { formal: TemplateKnob; bright: TemplateKnob; motion: TemplateKnob };
51
+
52
+ export type TemplateFont = { file: string; license: string };
53
+
54
+ export type DesignOverrides = {
55
+ palette?: Partial<DesignSystem['palette']>;
56
+ fonts?: Partial<DesignSystem['fonts']>;
57
+ typeScale?: Partial<DesignSystem['typeScale']>;
58
+ radius?: number;
59
+ };
60
+
61
+ export type TemplateSummary = {
62
+ id: string;
63
+ name: string;
64
+ occasions: OccasionId[];
65
+ audience: string;
66
+ object: string;
67
+ reason: string;
68
+ accent: string;
69
+ design: DesignSystem;
70
+ knobs: TemplateKnobs;
71
+ skeletons: SkeletonId[];
72
+ hasDemo: boolean;
73
+ pages: number;
74
+ /** 1-based sample pages shown in the library dialog; null means pick four. */
75
+ previewPages: number[] | null;
76
+ /** This template's own floor for small type, 16–20; absent means the default 20. */
77
+ minType?: number;
78
+ };
79
+
80
+ export type Template = Omit<TemplateSummary, 'hasDemo' | 'pages'> & {
81
+ fonts: TemplateFont[];
82
+ source: string | null;
83
+ };
84
+
85
+ export type AppliedTemplate = {
86
+ id: string;
87
+ knobs: TemplateKnobValues;
88
+ appliedAt: number;
89
+ };
90
+
91
+ export type TemplateRead = { ok: true; template: Template } | { ok: false; reason: string };
92
+
93
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
94
+ typeof value === 'object' && value !== null && !Array.isArray(value);
95
+
96
+ const text = (value: unknown): string | null =>
97
+ typeof value === 'string' && value.trim() !== '' ? value.trim() : null;
98
+
99
+ const KNOB_KEYS = ['formal', 'bright', 'motion'] as const;
100
+
101
+ function readDesign(raw: unknown): DesignSystem | null {
102
+ if (!isRecord(raw)) return null;
103
+ const palette = raw.palette;
104
+ const fonts = raw.fonts;
105
+ const typeScale = raw.typeScale;
106
+ if (!isRecord(palette) || !isRecord(fonts) || !isRecord(typeScale)) return null;
107
+ const bg = text(palette.bg);
108
+ const ink = text(palette.text);
109
+ const accent = text(palette.accent);
110
+ const display = text(fonts.display);
111
+ const body = text(fonts.body);
112
+ const hero = typeScale.hero;
113
+ const bodySize = typeScale.body;
114
+ const radius = raw.radius;
115
+ if (!bg || !ink || !accent || !display || !body) return null;
116
+ if (typeof hero !== 'number' || !Number.isFinite(hero) || hero <= 0) return null;
117
+ if (typeof bodySize !== 'number' || !Number.isFinite(bodySize) || bodySize <= 0) return null;
118
+ if (typeof radius !== 'number' || !Number.isFinite(radius) || radius < 0) return null;
119
+ return {
120
+ palette: { bg, text: ink, accent },
121
+ fonts: { display, body },
122
+ typeScale: { hero, body: bodySize },
123
+ radius,
124
+ };
125
+ }
126
+
127
+ function readOverrides(raw: unknown): DesignOverrides | undefined {
128
+ if (!isRecord(raw)) return undefined;
129
+ const out: DesignOverrides = {};
130
+ if (isRecord(raw.palette)) {
131
+ const palette: Partial<DesignSystem['palette']> = {};
132
+ for (const key of ['bg', 'text', 'accent'] as const) {
133
+ const value = text(raw.palette[key]);
134
+ if (value) palette[key] = value;
135
+ }
136
+ if (Object.keys(palette).length > 0) out.palette = palette;
137
+ }
138
+ if (isRecord(raw.fonts)) {
139
+ const fonts: Partial<DesignSystem['fonts']> = {};
140
+ for (const key of ['display', 'body'] as const) {
141
+ const value = text(raw.fonts[key]);
142
+ if (value) fonts[key] = value;
143
+ }
144
+ if (Object.keys(fonts).length > 0) out.fonts = fonts;
145
+ }
146
+ if (isRecord(raw.typeScale)) {
147
+ const typeScale: Partial<DesignSystem['typeScale']> = {};
148
+ for (const key of ['hero', 'body'] as const) {
149
+ const value = raw.typeScale[key];
150
+ if (typeof value === 'number' && Number.isFinite(value) && value > 0) typeScale[key] = value;
151
+ }
152
+ if (Object.keys(typeScale).length > 0) out.typeScale = typeScale;
153
+ }
154
+ const radius = raw.radius;
155
+ if (typeof radius === 'number' && Number.isFinite(radius) && radius >= 0) out.radius = radius;
156
+ return Object.keys(out).length > 0 ? out : undefined;
157
+ }
158
+
159
+ function readKnobEnd(raw: unknown): TemplateKnobEnd | null {
160
+ if (!isRecord(raw)) return null;
161
+ const label = text(raw.label);
162
+ if (!label) return null;
163
+ const design = readOverrides(raw.design);
164
+ const flags: Record<string, string | number | boolean> = {};
165
+ if (isRecord(raw.flags)) {
166
+ for (const [key, value] of Object.entries(raw.flags)) {
167
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
168
+ flags[key] = value;
169
+ }
170
+ }
171
+ }
172
+ const end: TemplateKnobEnd = { label };
173
+ if (design) end.design = design;
174
+ if (Object.keys(flags).length > 0) end.flags = flags;
175
+ return end;
176
+ }
177
+
178
+ function readKnobs(raw: unknown): TemplateKnobs | null {
179
+ if (!isRecord(raw)) return null;
180
+ const knobs: Partial<TemplateKnobs> = {};
181
+ for (const key of KNOB_KEYS) {
182
+ const knob = raw[key];
183
+ if (!isRecord(knob)) return null;
184
+ const left = readKnobEnd(knob.left);
185
+ const right = readKnobEnd(knob.right);
186
+ if (!left || !right) return null;
187
+ knobs[key] = { left, right };
188
+ }
189
+ return knobs as TemplateKnobs;
190
+ }
191
+
192
+ function readFonts(raw: unknown): TemplateFont[] | null {
193
+ if (!Array.isArray(raw)) return null;
194
+ const fonts: TemplateFont[] = [];
195
+ for (const entry of raw) {
196
+ if (!isRecord(entry)) return null;
197
+ const file = text(entry.file);
198
+ const license = text(entry.license);
199
+ if (!file || !license) return null;
200
+ fonts.push({ file, license });
201
+ }
202
+ return fonts;
203
+ }
204
+
205
+ /**
206
+ * Turns a `template.json` into a template, or names the first thing that is
207
+ * wrong with it. A template the library cannot trust is dropped whole rather
208
+ * than half-read.
209
+ */
210
+ export function sanitizeTemplate(raw: unknown, expectedId?: string): TemplateRead {
211
+ if (!isRecord(raw)) return { ok: false, reason: 'not a JSON object' };
212
+ const id = text(raw.id);
213
+ if (!id || !/^[a-z0-9][a-z0-9-]*$/.test(id)) {
214
+ return { ok: false, reason: 'id must be lowercase letters, digits and dashes' };
215
+ }
216
+ if (expectedId !== undefined && id !== expectedId) {
217
+ return { ok: false, reason: `id "${id}" does not match the folder "${expectedId}"` };
218
+ }
219
+ const name = text(raw.name);
220
+ if (!name) return { ok: false, reason: 'missing name' };
221
+ const audience = text(raw.audience);
222
+ if (!audience) return { ok: false, reason: 'missing audience' };
223
+ const object = text(raw.object);
224
+ if (!object) return { ok: false, reason: 'missing object' };
225
+ const reason = text(raw.reason);
226
+ if (!reason) return { ok: false, reason: 'missing reason' };
227
+ const accent = text(raw.accent);
228
+ if (!accent) return { ok: false, reason: 'missing accent' };
229
+
230
+ const occasionsRaw = raw.occasions;
231
+ if (!Array.isArray(occasionsRaw) || occasionsRaw.length === 0) {
232
+ return { ok: false, reason: 'missing occasions' };
233
+ }
234
+ const occasions: OccasionId[] = [];
235
+ for (const entry of occasionsRaw) {
236
+ if (typeof entry !== 'string' || !OCCASION_IDS.has(entry)) {
237
+ return { ok: false, reason: `unknown occasion "${String(entry)}"` };
238
+ }
239
+ if (!occasions.includes(entry as OccasionId)) occasions.push(entry as OccasionId);
240
+ }
241
+
242
+ const design = readDesign(raw.design);
243
+ if (!design) return { ok: false, reason: 'missing or malformed design' };
244
+
245
+ const knobs = readKnobs(raw.knobs);
246
+ if (!knobs) return { ok: false, reason: 'missing or malformed knobs' };
247
+
248
+ const skeletonsRaw = raw.skeletons;
249
+ if (!Array.isArray(skeletonsRaw) || skeletonsRaw.length === 0) {
250
+ return { ok: false, reason: 'missing skeletons' };
251
+ }
252
+ const skeletons: SkeletonId[] = [];
253
+ for (const entry of skeletonsRaw) {
254
+ if (typeof entry !== 'string' || !SKELETON_IDS.has(entry)) {
255
+ return { ok: false, reason: `unknown skeleton "${String(entry)}"` };
256
+ }
257
+ if (!skeletons.includes(entry as SkeletonId)) skeletons.push(entry as SkeletonId);
258
+ }
259
+
260
+ const fonts = readFonts(raw.fonts);
261
+ if (!fonts) return { ok: false, reason: 'missing or malformed fonts' };
262
+
263
+ const source = raw.source;
264
+ if (source !== null && source !== undefined && typeof source !== 'string') {
265
+ return { ok: false, reason: 'source must be a string or null' };
266
+ }
267
+
268
+ const minTypeRaw = raw.minType;
269
+ if (minTypeRaw !== undefined && minTypeRaw !== null) {
270
+ if (
271
+ typeof minTypeRaw !== 'number' ||
272
+ !Number.isInteger(minTypeRaw) ||
273
+ minTypeRaw < MIN_TYPE_FLOOR.min ||
274
+ minTypeRaw > MIN_TYPE_FLOOR.max
275
+ ) {
276
+ return {
277
+ ok: false,
278
+ reason: `minType must be an integer from ${MIN_TYPE_FLOOR.min} to ${MIN_TYPE_FLOOR.max}`,
279
+ };
280
+ }
281
+ }
282
+
283
+ const previewPages = Array.isArray(raw.previewPages)
284
+ ? raw.previewPages
285
+ .filter((n): n is number => typeof n === 'number' && Number.isInteger(n) && n >= 1)
286
+ .slice(0, 8)
287
+ : [];
288
+
289
+ return {
290
+ ok: true,
291
+ template: {
292
+ id,
293
+ name,
294
+ occasions,
295
+ audience,
296
+ object,
297
+ reason,
298
+ accent,
299
+ design,
300
+ knobs,
301
+ skeletons,
302
+ previewPages: previewPages.length > 0 ? previewPages : null,
303
+ minType: typeof minTypeRaw === 'number' ? minTypeRaw : undefined,
304
+ fonts,
305
+ source: typeof source === 'string' && source.trim() !== '' ? source.trim() : null,
306
+ },
307
+ };
308
+ }
309
+
310
+ /** The board record written when a template is applied; anything else is ignored. */
311
+ export function sanitizeAppliedTemplate(raw: unknown): AppliedTemplate | null {
312
+ if (!isRecord(raw)) return null;
313
+ const id = text(raw.id);
314
+ const appliedAt = raw.appliedAt;
315
+ if (!id || typeof appliedAt !== 'number' || !Number.isFinite(appliedAt)) return null;
316
+ return { id, knobs: sanitizeKnobValues(raw.knobs), appliedAt };
317
+ }
318
+
319
+ export function sanitizeKnobValues(raw: unknown): TemplateKnobValues {
320
+ if (!isRecord(raw)) return {};
321
+ const values: TemplateKnobValues = {};
322
+ for (const key of KNOB_KEYS) {
323
+ const value = raw[key];
324
+ if (typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= 1) {
325
+ values[key] = value;
326
+ }
327
+ }
328
+ return values;
329
+ }
330
+
331
+ /**
332
+ * Values under a third lean left, over two thirds lean right; the middle keeps
333
+ * the template's own design. Discrete ends keep a deck's colours reproducible.
334
+ */
335
+ export { applyKnobs, knobFlags } from './knobs.ts';
336
+
337
+ export function designSnippet(design: DesignSystem): string {
338
+ return `export const design: DesignSystem = ${JSON.stringify(design, null, 2)};`;
339
+ }
@@ -0,0 +1,117 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { type PreviewOutline, previewContent } from './preview-content.ts';
3
+
4
+ const outline = (over: Partial<PreviewOutline> = {}): PreviewOutline => ({
5
+ brief: '给董事会讲第三季度的经营情况。收入超了计划。',
6
+ audience: '董事会',
7
+ items: [
8
+ { title: '第三季度经营情况', kind: 'cover' },
9
+ { title: '收入 4.82 亿,超计划 1,700 万', kind: 'kpi' },
10
+ { title: '增长来自哪里', kind: 'list' },
11
+ ],
12
+ ...over,
13
+ });
14
+
15
+ const NOW = new Date('2026-10-31T09:00:00+08:00');
16
+
17
+ describe('previewContent', () => {
18
+ it('draws the cover from the outline and the date and audience line', () => {
19
+ const { cover } = previewContent(outline(), { now: NOW, locale: 'zh-CN' });
20
+ expect(cover.title).toBe('第三季度经营情况');
21
+ expect(cover.subtitle).toBe('给董事会讲第三季度的经营情况。');
22
+ expect(cover.meta).toContain('2026');
23
+ expect(cover.meta).toContain('董事会');
24
+ });
25
+
26
+ it('leaves the audience out of the meta line when it is empty', () => {
27
+ const { cover } = previewContent(outline({ audience: '' }), { now: NOW, locale: 'zh-CN' });
28
+ expect(cover.meta).not.toContain('·');
29
+ });
30
+
31
+ it.each([
32
+ ['收入 4.82 亿', '4.82', '亿'],
33
+ ['预算 1,700 万', '1,700', '万'],
34
+ ['毛利率 32%', '32', '%'],
35
+ ['效率提升 3.2 倍', '3.2', '倍'],
36
+ ])('reads the number and unit out of %s', (title, value, unit) => {
37
+ const page = { title, kind: 'kpi' };
38
+ expect(previewContent(outline({ items: [page] })).data).toMatchObject({ value, unit });
39
+ });
40
+
41
+ it('takes the number from the brief when the page title has none', () => {
42
+ const data = previewContent(
43
+ outline({
44
+ items: [
45
+ { title: '第三季度经营情况', kind: 'cover' },
46
+ { title: '这一季度的成绩', kind: 'kpi' },
47
+ ],
48
+ }),
49
+ ).data;
50
+ expect(data.value).toBe('');
51
+ const withNumber = previewContent(
52
+ outline({
53
+ brief: '给董事会讲第三季度的经营情况,收入 4.82 亿。',
54
+ items: [
55
+ { title: '第三季度经营情况', kind: 'cover' },
56
+ { title: '这一季度的成绩', kind: 'kpi' },
57
+ ],
58
+ }),
59
+ ).data;
60
+ expect(withNumber).toMatchObject({ value: '4.82', unit: '亿', sample: false });
61
+ });
62
+
63
+ it('marks the number as a sample when the outline has none at all', () => {
64
+ const data = previewContent(
65
+ outline({
66
+ brief: '讲讲我们这三个月的进展。',
67
+ items: [
68
+ { title: '季度进展', kind: 'cover' },
69
+ { title: '这一季度的成绩', kind: 'kpi' },
70
+ ],
71
+ }),
72
+ ).data;
73
+ expect(data.sample).toBe(true);
74
+ expect(data.value).toBe('');
75
+ });
76
+
77
+ it('truncates a title that would not fit the preview', () => {
78
+ const long = '第三季度经营情况与下一阶段的重点工作和资源安排建议';
79
+ const { cover, data } = previewContent(
80
+ outline({
81
+ items: [
82
+ { title: long, kind: 'cover' },
83
+ { title: `${long} 4.82 亿`, kind: 'kpi' },
84
+ ],
85
+ }),
86
+ );
87
+ expect(cover.title).toHaveLength(24);
88
+ expect(cover.title.endsWith('…')).toBe(true);
89
+ expect(data.title).toHaveLength(24);
90
+ });
91
+
92
+ it.each([
93
+ ['第三季度经营情况', 'Q3'],
94
+ ['Q4 复盘', 'Q4'],
95
+ ['FY26 全年计划', 'FY26'],
96
+ ['2027 财年的目标', 'FY27'],
97
+ ['2026 年经营情况', '2026'],
98
+ ])('takes the mark from a title that names %s', (title, mark) => {
99
+ const made = outline({ items: [{ title, kind: 'cover' }], brief: '没有别的线索。' });
100
+ expect(previewContent(made).cover.mark).toBe(mark);
101
+ });
102
+
103
+ it('falls back to the brief for the mark, and then leaves it empty', () => {
104
+ const fromBrief = outline({
105
+ items: [{ title: '经营情况', kind: 'cover' }],
106
+ brief: '给董事会讲第三季度。',
107
+ });
108
+ expect(previewContent(fromBrief).cover.mark).toBe('Q3');
109
+
110
+ const none = outline({
111
+ items: [{ title: '经营情况', kind: 'cover' }],
112
+ brief: '给董事会讲这半年的进展。',
113
+ });
114
+ expect(previewContent(none).cover.mark).toBe('');
115
+ expect(previewContent(none).cover.brand).toBe('');
116
+ });
117
+ });