|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The app-scoped record page — `PageSchema.apps` (maintainer ruling A on |
| 5 | + * #28979, record 6573557633). |
| 6 | + * |
| 7 | + * A record page names the apps whose routes open it for its object. Inside |
| 8 | + * one of them that page opens; everywhere else, Setup included, the object's |
| 9 | + * one `isDefault` record page still opens. The rule is the default's one level |
| 10 | + * down: at most one record page per object PER APP, refused by `defineStack` |
| 11 | + * and `composeStacks` the way a second default is (the registry's package door |
| 12 | + * and the metadata save door are pinned in their own packages), naming both |
| 13 | + * pages. The default and an app's page are two slots and coexist. |
| 14 | + * |
| 15 | + * Refusals assert the ADR-0112 envelope (`code` + `status`), the issue count |
| 16 | + * and the named subjects; every refusal row has a control that differs from |
| 17 | + * it by one key alone. |
| 18 | + */ |
| 19 | +import { describe, expect, it } from 'vitest'; |
| 20 | +import { composeStacks, defineStack } from './stack.zod'; |
| 21 | +import { defaultRecordPageObject, misplacedDefaultPageRefusal } from './ui/default-record-page'; |
| 22 | +import { PageSchema } from './ui/page.zod'; |
| 23 | + |
| 24 | +type Refusal = Error & { code?: string; status?: number; issues?: readonly unknown[] }; |
| 25 | + |
| 26 | +const manifest = (id: string) => ({ |
| 27 | + id, |
| 28 | + namespace: id.split('.').pop()!.replace(/-/g, ''), |
| 29 | + name: id.split('.').pop()!, |
| 30 | + version: '1.0.0', |
| 31 | + type: 'module' as const, |
| 32 | +}); |
| 33 | + |
| 34 | +/** A record page of `object`; `apps` and `isDefault` are the keys a case varies. */ |
| 35 | +const recordPage = (name: string, object: string, extra: Record<string, unknown> = {}) => ({ |
| 36 | + name, |
| 37 | + label: name, |
| 38 | + type: 'record' as const, |
| 39 | + object, |
| 40 | + regions: [], |
| 41 | + ...extra, |
| 42 | +}); |
| 43 | + |
| 44 | +const refusalOf = (fn: () => unknown): Refusal | undefined => { |
| 45 | + try { |
| 46 | + fn(); |
| 47 | + return undefined; |
| 48 | + } catch (e) { |
| 49 | + return e as Refusal; |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +const stack = (pages: unknown[], id = 'com.example.arp') => ({ manifest: manifest(id), pages }) as any; |
| 54 | + |
| 55 | +const issuesOf = (value: unknown) => { |
| 56 | + const r = PageSchema.safeParse(value); |
| 57 | + return r.success ? [] : r.error.issues.map((i) => ({ code: i.code, path: i.path.join('.'), message: i.message })); |
| 58 | +}; |
| 59 | + |
| 60 | +describe('PageSchema.apps — the key', () => { |
| 61 | + it('a record page bound to an object takes `apps`, alone or beside `isDefault`', () => { |
| 62 | + expect(issuesOf(recordPage('member', 'sys_user', { apps: ['duly'] }))).toEqual([]); |
| 63 | + expect(issuesOf(recordPage('member', 'sys_user', { apps: ['duly', 'crm'], isDefault: true }))).toEqual([]); |
| 64 | + expect(PageSchema.parse(recordPage('member', 'sys_user', { apps: ['duly'] })).apps).toEqual(['duly']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('absent is unchanged — the parse adds nothing', () => { |
| 68 | + expect(PageSchema.parse(recordPage('member', 'sys_user')).apps).toBeUndefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('an empty list is refused (a scope with no app decides nothing)', () => { |
| 72 | + expect(issuesOf(recordPage('member', 'sys_user', { apps: [] })).map((i) => [i.code, i.path])).toEqual([['too_small', 'apps']]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('an app listed twice is refused at the repeat', () => { |
| 76 | + const issues = issuesOf(recordPage('member', 'sys_user', { apps: ['duly', 'crm', 'duly'] })); |
| 77 | + expect(issues.map((i) => [i.code, i.path])).toEqual([['custom', 'apps.2']]); |
| 78 | + expect(issues[0]!.message).toContain("App 'duly' is listed twice in `apps`"); |
| 79 | + }); |
| 80 | + |
| 81 | + it('an entry that is not an app name (lowercase snake_case) is refused', () => { |
| 82 | + expect(issuesOf(recordPage('member', 'sys_user', { apps: ['Duly App'] })).map((i) => i.path)).toEqual(['apps.0']); |
| 83 | + }); |
| 84 | + |
| 85 | + it.each([ |
| 86 | + ['a home page', { name: 'pg', label: 'P', type: 'home', apps: ['duly'] }, "`type: 'home'`"], |
| 87 | + ['an app page', { name: 'pg', label: 'P', type: 'app', apps: ['duly'] }, "`type: 'app'`"], |
| 88 | + ['a record page bound to no object', { name: 'pg', label: 'P', type: 'record', apps: ['duly'] }, 'bound to no `object`'], |
| 89 | + ])('is refused on %s, at `apps`, in the registry door\'s sentence', (_label, page, subject) => { |
| 90 | + const issues = issuesOf(page); |
| 91 | + expect(issues.map((i) => [i.code, i.path])).toEqual([['custom', 'apps']]); |
| 92 | + expect(issues[0]!.message).toContain(subject); |
| 93 | + expect(issues[0]!.message).toBe(misplacedDefaultPageRefusal(page)); |
| 94 | + }); |
| 95 | + |
| 96 | + it('a misplaced page carrying both keys is told about both, each at its own path', () => { |
| 97 | + const issues = issuesOf({ name: 'pg', label: 'P', type: 'home', isDefault: true, apps: ['duly'] }); |
| 98 | + expect(issues.map((i) => i.path)).toEqual(['isDefault', 'apps']); |
| 99 | + }); |
| 100 | + |
| 101 | + it('the describe carries the planned marker and no tracker number', () => { |
| 102 | + const description = PageSchema.shape.apps.description ?? ''; |
| 103 | + expect(description).toMatch(/^\[PLANNED — not yet rendered\] /); |
| 104 | + expect(description).not.toMatch(/#\d{3,}/); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('defaultRecordPageObject(page, app) — the one reading of an app\'s slot', () => { |
| 109 | + it('names the object of a record page scoped to the app', () => { |
| 110 | + expect(defaultRecordPageObject(recordPage('member', 'sys_user', { apps: ['duly'] }), 'duly')).toBe('sys_user'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('answers undefined for an app the page does not name, and for the default slot when not isDefault', () => { |
| 114 | + const page = recordPage('member', 'sys_user', { apps: ['duly'] }); |
| 115 | + expect(defaultRecordPageObject(page, 'setup')).toBeUndefined(); |
| 116 | + expect(defaultRecordPageObject(page)).toBeUndefined(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('a default page holds no app slot unless it names the app — the two slots are apart', () => { |
| 120 | + const page = recordPage('user_detail', 'sys_user', { isDefault: true }); |
| 121 | + expect(defaultRecordPageObject(page)).toBe('sys_user'); |
| 122 | + expect(defaultRecordPageObject(page, 'duly')).toBeUndefined(); |
| 123 | + }); |
| 124 | + |
| 125 | + it.each([ |
| 126 | + ['a home page', { name: 'p', type: 'home', object: 'sys_user', apps: ['duly'] }], |
| 127 | + ['a record page bound to no object', { name: 'p', type: 'record', apps: ['duly'] }], |
| 128 | + ['`apps` that is not an array', { name: 'p', type: 'record', object: 'sys_user', apps: 'duly' }], |
| 129 | + ])('answers undefined for %s', (_label, page) => { |
| 130 | + expect(defaultRecordPageObject(page, 'duly')).toBeUndefined(); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe('defineStack — two record pages for one object in one app', () => { |
| 135 | + it('is refused, naming the object, the app, both pages and their positions', () => { |
| 136 | + const err = refusalOf(() => |
| 137 | + defineStack(stack([ |
| 138 | + recordPage('member_a', 'sys_user', { apps: ['duly'] }), |
| 139 | + recordPage('member_b', 'sys_user', { apps: ['duly'] }), |
| 140 | + ])), |
| 141 | + ); |
| 142 | + expect(err, 'the stack was refused').toBeDefined(); |
| 143 | + expect({ code: err!.code, status: err!.status }).toEqual({ code: 'STACK_CROSS_REFERENCE_INVALID', status: 422 }); |
| 144 | + expect(err!.issues).toHaveLength(1); |
| 145 | + expect(err!.issues![0]).toMatch( |
| 146 | + /^Object 'sys_user' has two record pages for app 'duly' in `pages`: 'member_a' \(stack\.pages\[0\]\) and 'member_b' \(stack\.pages\[1\]\)\. Remove 'duly' from `apps` on all but one of them\./, |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('control: the same two pages scoped to different apps are accepted', () => { |
| 151 | + expect(() => |
| 152 | + defineStack(stack([ |
| 153 | + recordPage('member_a', 'sys_user', { apps: ['duly'] }), |
| 154 | + recordPage('member_b', 'sys_user', { apps: ['crm'] }), |
| 155 | + ])), |
| 156 | + ).not.toThrow(); |
| 157 | + }); |
| 158 | + |
| 159 | + it('a page scoped to two apps holds both: a second page in either app is refused, the other app untouched', () => { |
| 160 | + const err = refusalOf(() => |
| 161 | + defineStack(stack([ |
| 162 | + recordPage('member_a', 'sys_user', { apps: ['duly', 'crm'] }), |
| 163 | + recordPage('member_b', 'sys_user', { apps: ['crm'] }), |
| 164 | + ])), |
| 165 | + ); |
| 166 | + expect(err?.code).toBe('STACK_CROSS_REFERENCE_INVALID'); |
| 167 | + expect(err!.issues).toHaveLength(1); |
| 168 | + expect(err!.issues![0]).toMatch(/^Object 'sys_user' has two record pages for app 'crm' in `pages`/); |
| 169 | + }); |
| 170 | + |
| 171 | + it('control: a page scoped to two apps alone is accepted', () => { |
| 172 | + expect(() => defineStack(stack([recordPage('member_a', 'sys_user', { apps: ['duly', 'crm'] })]))).not.toThrow(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('the default and an app\'s page for one object coexist — two slots, no precedence', () => { |
| 176 | + expect(() => |
| 177 | + defineStack(stack([ |
| 178 | + recordPage('user_detail', 'sys_user', { isDefault: true }), |
| 179 | + recordPage('member', 'sys_user', { apps: ['duly'] }), |
| 180 | + ])), |
| 181 | + ).not.toThrow(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('one page may be the default and an app\'s page at once', () => { |
| 185 | + expect(() => |
| 186 | + defineStack(stack([ |
| 187 | + recordPage('user_detail', 'sys_user', { isDefault: true, apps: ['setup'] }), |
| 188 | + recordPage('member', 'sys_user', { apps: ['duly'] }), |
| 189 | + ])), |
| 190 | + ).not.toThrow(); |
| 191 | + }); |
| 192 | + |
| 193 | + it('a second default is still refused as a default, never as an app\'s page', () => { |
| 194 | + const err = refusalOf(() => |
| 195 | + defineStack(stack([ |
| 196 | + recordPage('user_detail', 'sys_user', { isDefault: true }), |
| 197 | + recordPage('member', 'sys_user', { isDefault: true, apps: ['duly'] }), |
| 198 | + ])), |
| 199 | + ); |
| 200 | + expect(err!.issues).toHaveLength(1); |
| 201 | + expect(err!.issues![0]).toMatch(/^Object 'sys_user' has two default record pages in `pages`/); |
| 202 | + }); |
| 203 | +}); |
| 204 | + |
| 205 | +describe('composeStacks — two record pages for one object in one app across the stacks', () => { |
| 206 | + const a = () => defineStack(stack([recordPage('member_a', 'sys_user', { apps: ['duly'] })], 'com.example.arp-a')); |
| 207 | + const b = (page: unknown = recordPage('member_b', 'sys_user', { apps: ['duly'] })) => |
| 208 | + defineStack(stack([page], 'com.example.arp-b')); |
| 209 | + |
| 210 | + it('is refused, naming both stacks\' pages and the app', () => { |
| 211 | + const err = refusalOf(() => composeStacks([a(), b()])); |
| 212 | + expect({ code: err?.code, status: err?.status }).toEqual({ code: 'STACK_CROSS_REFERENCE_INVALID', status: 422 }); |
| 213 | + expect(err!.issues).toHaveLength(1); |
| 214 | + expect(err!.issues![0]).toContain( |
| 215 | + "Object 'sys_user' has two record pages for app 'duly' in the composed stacks: " |
| 216 | + + "'member_a' ('com.example.arp-a' (stack #0) pages[0]) and 'member_b' ('com.example.arp-b' (stack #1) pages[0])", |
| 217 | + ); |
| 218 | + }); |
| 219 | + |
| 220 | + it('is refused under `manifest: \'preserve\'` too', () => { |
| 221 | + expect(refusalOf(() => composeStacks([a(), b()], { manifest: 'preserve' }))?.code).toBe('STACK_CROSS_REFERENCE_INVALID'); |
| 222 | + }); |
| 223 | + |
| 224 | + it('control: the second stack\'s page scoped to another app composes', () => { |
| 225 | + expect(() => composeStacks([a(), b(recordPage('member_b', 'sys_user', { apps: ['crm'] }))])).not.toThrow(); |
| 226 | + }); |
| 227 | + |
| 228 | + it('control: the second stack\'s page the default instead composes beside the app\'s page', () => { |
| 229 | + expect(() => composeStacks([a(), b(recordPage('member_b', 'sys_user', { isDefault: true }))])).not.toThrow(); |
| 230 | + }); |
| 231 | +}); |
0 commit comments