|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Pin for the build-progress PHASE vocabulary (cloud#2172 ruling A). |
| 5 | + * |
| 6 | + * What is actually at stake here is not "does zod reject a bad string" — it is |
| 7 | + * that this vocabulary is CLOSED and that its refusal is LOUD. objectui#7388 |
| 8 | + * measured the cost of the alternative: the consumer's reader coerces any |
| 9 | + * value it does not recognise to `'structure'`, so a phase nobody declared |
| 10 | + * renders as a "still building" spinner for the 111 seconds the build spends |
| 11 | + * being verified after it finished. A refusal that merely throws leaves the |
| 12 | + * next author guessing; a refusal that names the accepted set tells them. |
| 13 | + * |
| 14 | + * So the refusal leg asserts the ENVELOPE, not the throw: zod 4 answers an |
| 15 | + * out-of-vocabulary enum value with `invalid_value` and carries the full |
| 16 | + * accepted set on the issue. ⛔ A bare `expect(...).toThrow()` here would stay |
| 17 | + * green against a schema that had degraded to a bare `z.string()` with a |
| 18 | + * refinement, which is exactly the degradation worth catching. |
| 19 | + * |
| 20 | + * The membership leg is the closed-enum pin. Each member was measured against |
| 21 | + * a real end of the channel (provenance is recorded per member on |
| 22 | + * `BUILD_PROGRESS_PHASES` in the source), so this array moving is a claim that |
| 23 | + * someone re-measured — not an edit to wave through. |
| 24 | + */ |
| 25 | + |
| 26 | +import { describe, it, expect } from 'vitest'; |
| 27 | + |
| 28 | +import { |
| 29 | + BUILD_PROGRESS_PHASES, |
| 30 | + BUILD_PROGRESS_FRAME_TYPE, |
| 31 | + BuildProgressPhaseSchema, |
| 32 | + BuildProgressFrameSchema, |
| 33 | + type BuildProgressPhase, |
| 34 | +} from './build-progress.zod'; |
| 35 | + |
| 36 | +/** |
| 37 | + * The phases objectui's reader discriminates TODAY |
| 38 | + * (`packages/plugin-chatbot/src/mapMessages.ts` `extractBuildProgress`, and the |
| 39 | + * `ChatBuildProgress['phase']` union in `ChatbotEnhanced.tsx`). Declaring |
| 40 | + * `verify` must not disturb the inherited three — this is the control that has |
| 41 | + * to keep moving, not a restatement of the vocabulary under test. |
| 42 | + */ |
| 43 | +const PHASES_THE_CONSUMER_ALREADY_RENDERS = ['structure', 'data', 'done'] as const; |
| 44 | + |
| 45 | +describe('BUILD_PROGRESS_PHASES (closed membership)', () => { |
| 46 | + it('is exactly the measured vocabulary, in lifecycle order', () => { |
| 47 | + expect(BUILD_PROGRESS_PHASES).toEqual(['structure', 'data', 'verify', 'done']); |
| 48 | + }); |
| 49 | + |
| 50 | + it('still contains every phase the objectui reader already discriminates', () => { |
| 51 | + for (const phase of PHASES_THE_CONSUMER_ALREADY_RENDERS) { |
| 52 | + expect(BUILD_PROGRESS_PHASES).toContain(phase); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + it('names the post-apply verification phase the card exists to declare', () => { |
| 57 | + expect(BUILD_PROGRESS_PHASES).toContain('verify'); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('BuildProgressPhaseSchema', () => { |
| 62 | + it('parses every declared phase', () => { |
| 63 | + for (const phase of BUILD_PROGRESS_PHASES) { |
| 64 | + const result = BuildProgressPhaseSchema.safeParse(phase); |
| 65 | + expect(result.success).toBe(true); |
| 66 | + expect(result.success && result.data).toBe(phase); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it('REFUSES an undeclared phase, and the refusal names the accepted set', () => { |
| 71 | + const result = BuildProgressPhaseSchema.safeParse('rebuilding'); |
| 72 | + |
| 73 | + expect(result.success).toBe(false); |
| 74 | + if (result.success) throw new Error('unreachable: an undeclared phase parsed'); |
| 75 | + |
| 76 | + const [issue] = result.error.issues; |
| 77 | + expect(result.error.issues).toHaveLength(1); |
| 78 | + expect(issue.code).toBe('invalid_value'); |
| 79 | + // The loud half: the author is told what WAS allowed, not merely that they |
| 80 | + // were wrong. Compared against the exported vocabulary so the two cannot |
| 81 | + // drift apart silently. |
| 82 | + expect((issue as { values?: readonly unknown[] }).values).toEqual([...BUILD_PROGRESS_PHASES]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('REFUSES the neighbouring vocabulary of the sibling blueprint-progress frame', () => { |
| 86 | + // `data-blueprint-progress` is a DIFFERENT channel with its own phases |
| 87 | + // (`designing` / `done`). Folding its vocabulary in here would be the |
| 88 | + // "one enum for two frames" mistake; `designing` must not parse. |
| 89 | + expect(BuildProgressPhaseSchema.safeParse('designing').success).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it('REFUSES a non-string', () => { |
| 93 | + expect(BuildProgressPhaseSchema.safeParse(3).success).toBe(false); |
| 94 | + expect(BuildProgressPhaseSchema.safeParse(undefined).success).toBe(false); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('BUILD_PROGRESS_FRAME_TYPE', () => { |
| 99 | + it('is the literal both ends select on', () => { |
| 100 | + // objectui selects parts with `p.type === 'data-build-progress'`; the |
| 101 | + // `data-` prefix is what makes it a custom data part at all. |
| 102 | + expect(BUILD_PROGRESS_FRAME_TYPE).toBe('data-build-progress'); |
| 103 | + expect(BUILD_PROGRESS_FRAME_TYPE.startsWith('data-')).toBe(true); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('BuildProgressFrameSchema', () => { |
| 108 | + it('parses a frame carrying only the required phase', () => { |
| 109 | + const result = BuildProgressFrameSchema.safeParse({ phase: 'verify' }); |
| 110 | + expect(result.success).toBe(true); |
| 111 | + expect(result.success && result.data.phase).toBe('verify'); |
| 112 | + expect(result.success && result.data.hop).toBeUndefined(); |
| 113 | + expect(result.success && result.data.tool).toBeUndefined(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('parses a frame carrying both optional fields', () => { |
| 117 | + const result = BuildProgressFrameSchema.safeParse({ |
| 118 | + phase: 'verify', |
| 119 | + hop: 4, |
| 120 | + tool: 'verify_build', |
| 121 | + }); |
| 122 | + expect(result.success).toBe(true); |
| 123 | + expect(result.success && result.data).toMatchObject({ |
| 124 | + phase: 'verify', |
| 125 | + hop: 4, |
| 126 | + tool: 'verify_build', |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('parses a frame carrying each optional field on its own', () => { |
| 131 | + expect(BuildProgressFrameSchema.safeParse({ phase: 'data', hop: 0 }).success).toBe(true); |
| 132 | + expect(BuildProgressFrameSchema.safeParse({ phase: 'data', tool: 'create_seed' }).success).toBe(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it('is a FLOOR: the panel fields objectui already reads survive the parse', () => { |
| 136 | + // Regression guard for "tighten it to .strict()". Every key below is read |
| 137 | + // by `extractBuildProgress` off this same frame today; refusing or |
| 138 | + // stripping them would break the shipping consumer. |
| 139 | + const shipping = { |
| 140 | + phase: 'structure' as const, |
| 141 | + appLabel: 'CRM', |
| 142 | + items: [{ type: 'object', name: 'contacts' }], |
| 143 | + done: 1, |
| 144 | + total: 4, |
| 145 | + seq: 12, |
| 146 | + }; |
| 147 | + |
| 148 | + const result = BuildProgressFrameSchema.safeParse(shipping); |
| 149 | + expect(result.success).toBe(true); |
| 150 | + expect(result.success && result.data).toMatchObject(shipping); |
| 151 | + }); |
| 152 | + |
| 153 | + it('REFUSES an undeclared phase, locating it at `phase`', () => { |
| 154 | + const result = BuildProgressFrameSchema.safeParse({ phase: 'rebuilding', hop: 1 }); |
| 155 | + |
| 156 | + expect(result.success).toBe(false); |
| 157 | + if (result.success) throw new Error('unreachable: a frame with an undeclared phase parsed'); |
| 158 | + |
| 159 | + const [issue] = result.error.issues; |
| 160 | + expect(issue.code).toBe('invalid_value'); |
| 161 | + expect(issue.path).toEqual(['phase']); |
| 162 | + }); |
| 163 | + |
| 164 | + it('REFUSES a frame with no phase at all', () => { |
| 165 | + const result = BuildProgressFrameSchema.safeParse({ hop: 2, tool: 'verify_build' }); |
| 166 | + expect(result.success).toBe(false); |
| 167 | + expect(result.success === false && result.error.issues[0].path).toEqual(['phase']); |
| 168 | + }); |
| 169 | + |
| 170 | + it('REFUSES a malformed hop and an empty tool name', () => { |
| 171 | + expect(BuildProgressFrameSchema.safeParse({ phase: 'verify', hop: -1 }).success).toBe(false); |
| 172 | + expect(BuildProgressFrameSchema.safeParse({ phase: 'verify', hop: 1.5 }).success).toBe(false); |
| 173 | + expect(BuildProgressFrameSchema.safeParse({ phase: 'verify', tool: '' }).success).toBe(false); |
| 174 | + }); |
| 175 | +}); |
| 176 | + |
| 177 | +describe('BuildProgressPhase (type)', () => { |
| 178 | + it('admits every declared member and is assignable from the array', () => { |
| 179 | + const everyPhase: BuildProgressPhase[] = [...BUILD_PROGRESS_PHASES]; |
| 180 | + expect(everyPhase).toHaveLength(BUILD_PROGRESS_PHASES.length); |
| 181 | + }); |
| 182 | +}); |
0 commit comments