|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | + |
| 5 | +import { PageVariablesPage } from '../src/pages/index.js'; |
| 6 | +import stack from '../objectstack.config.js'; |
| 7 | +import { ShowcaseApp } from '../src/apps/index.js'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Dogfood gate for page-local state (PageSchema.variables, ADR-0049). |
| 11 | + * |
| 12 | + * In the "demonstrated AND verified" spirit: it is not enough that the page |
| 13 | + * *declares* a variable and a picker. These assertions prove the wiring is |
| 14 | + * coherent end-to-end — the variable names a real writer component, the gating |
| 15 | + * predicates reference that variable, and (crucially) the predicates actually |
| 16 | + * gate the way the demo claims when the variable flips. A page that merely |
| 17 | + * looked plausible but mis-wired the `source` id or inverted a predicate would |
| 18 | + * pass a shape-only check but fail here. |
| 19 | + */ |
| 20 | + |
| 21 | +type AnyComponent = { |
| 22 | + type: string; |
| 23 | + id?: string; |
| 24 | + visibility?: unknown; |
| 25 | + [k: string]: unknown; |
| 26 | +}; |
| 27 | + |
| 28 | +/** Flatten every component across the page's regions. */ |
| 29 | +function allComponents(page: typeof PageVariablesPage): AnyComponent[] { |
| 30 | + const out: AnyComponent[] = []; |
| 31 | + for (const region of page.regions ?? []) { |
| 32 | + for (const c of region.components ?? []) out.push(c as AnyComponent); |
| 33 | + } |
| 34 | + return out; |
| 35 | +} |
| 36 | + |
| 37 | +/** Extract a predicate's CEL source whether stored as a bare string or the |
| 38 | + * normalized `{ dialect, source }` envelope that definePage produces. */ |
| 39 | +function predicateSource(visibility: unknown): string | undefined { |
| 40 | + if (typeof visibility === 'string') return visibility; |
| 41 | + if (visibility && typeof visibility === 'object' && typeof (visibility as any).source === 'string') { |
| 42 | + return (visibility as any).source; |
| 43 | + } |
| 44 | + return undefined; |
| 45 | +} |
| 46 | + |
| 47 | +/** Evaluate a (simple, comparison-only) CEL predicate against a page scope. |
| 48 | + * Sufficient for the `==` / `!=` predicates this page uses. */ |
| 49 | +function evalPredicate(source: string, page: Record<string, unknown>): boolean { |
| 50 | + // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func |
| 51 | + const fn = new Function('page', `"use strict"; return (${source});`) as ( |
| 52 | + p: Record<string, unknown>, |
| 53 | + ) => boolean; |
| 54 | + return Boolean(fn(page)); |
| 55 | +} |
| 56 | + |
| 57 | +describe('Page Variables showcase — page-local state (ADR-0049)', () => { |
| 58 | + it('parses and declares the selectedProjectId variable bound to the picker', () => { |
| 59 | + expect(PageVariablesPage.name).toBe('showcase_page_variables'); |
| 60 | + |
| 61 | + const vars = PageVariablesPage.variables ?? []; |
| 62 | + const sel = vars.find((v) => v.name === 'selectedProjectId'); |
| 63 | + expect(sel, 'selectedProjectId variable must exist').toBeTruthy(); |
| 64 | + expect(sel!.type).toBe('record_id'); |
| 65 | + // source names the WRITER component id. |
| 66 | + expect(sel!.source).toBe('project_picker'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('ships a record picker whose id matches the variable source', () => { |
| 70 | + const picker = allComponents(PageVariablesPage).find((c) => c.id === 'project_picker'); |
| 71 | + expect(picker, 'a component with id project_picker must exist').toBeTruthy(); |
| 72 | + expect(picker!.type).toBe('element:record_picker'); |
| 73 | + // It binds to a real object so the picker has something to load. |
| 74 | + expect((picker as any).dataSource?.object).toBe('showcase_project'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('gates its detail panel on the variable — hidden until a project is picked, shown after', () => { |
| 78 | + const comps = allComponents(PageVariablesPage); |
| 79 | + const gated = comps.filter((c) => c.visibility !== undefined); |
| 80 | + // Empty-hint + divider + heading + body — every gated node references the variable. |
| 81 | + expect(gated.length).toBeGreaterThanOrEqual(2); |
| 82 | + |
| 83 | + const empty = { page: { selectedProjectId: '' } as Record<string, unknown> }; |
| 84 | + const picked = { page: { selectedProjectId: 'proj_42' } as Record<string, unknown> }; |
| 85 | + |
| 86 | + let shownWhenEmpty = 0; |
| 87 | + let shownWhenPicked = 0; |
| 88 | + for (const c of gated) { |
| 89 | + const src = predicateSource(c.visibility); |
| 90 | + expect(src, `gated component ${c.id} must carry a predicate`).toBeTruthy(); |
| 91 | + // Every gating predicate is about the page variable. |
| 92 | + expect(src).toContain('page.selectedProjectId'); |
| 93 | + if (evalPredicate(src!, empty.page)) shownWhenEmpty++; |
| 94 | + if (evalPredicate(src!, picked.page)) shownWhenPicked++; |
| 95 | + } |
| 96 | + |
| 97 | + // The empty-state hint shows only when nothing is picked; the detail panel |
| 98 | + // (divider + heading + body) shows only after a pick. So the visible set |
| 99 | + // strictly flips between the two states — proving the variable drives the UI. |
| 100 | + expect(shownWhenEmpty).toBeGreaterThanOrEqual(1); // the empty hint |
| 101 | + expect(shownWhenPicked).toBeGreaterThanOrEqual(1); // the detail panel |
| 102 | + // The empty-state predicate and the detail predicates are mutually exclusive: |
| 103 | + // no gated node is visible in BOTH states. |
| 104 | + for (const c of gated) { |
| 105 | + const src = predicateSource(c.visibility)!; |
| 106 | + const inEmpty = evalPredicate(src, empty.page); |
| 107 | + const inPicked = evalPredicate(src, picked.page); |
| 108 | + expect(inEmpty && inPicked, `component ${c.id} should not be visible in both states`).toBe(false); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + it('is registered in the app config and reachable from navigation', () => { |
| 113 | + const pageNames = (stack.pages ?? []).map((p: any) => p.name); |
| 114 | + expect(pageNames).toContain('showcase_page_variables'); |
| 115 | + |
| 116 | + // Navigation has a link to the page. |
| 117 | + const flat = JSON.stringify(ShowcaseApp.navigation ?? []); |
| 118 | + expect(flat).toContain('showcase_page_variables'); |
| 119 | + }); |
| 120 | +}); |
0 commit comments