|
| 1 | +// #9566 / #9474 — `onSuccess` post-success navigation (maintainer ruling |
| 2 | +// 2026-08-18, recorded on #9566): one CLOSED key covering both server-executing |
| 3 | +// action types, `navigate` (route/URL template whose scope gains `${result.*}`, |
| 4 | +// the server response) + `openIn: 'self' | 'newTab'` defaulting `'self'`. |
| 5 | +// These pins hold the ruled shape: the accept set, the materialized default, |
| 6 | +// the closed enum, the strict inner object, and the api/script type scope. |
| 7 | +import { describe, it, expect } from 'vitest'; |
| 8 | +import { ActionSchema, InlineActionSchema } from './action.zod'; |
| 9 | +import { getMetadataTypeSchema } from '../kernel/metadata-type-schemas'; |
| 10 | + |
| 11 | +const base = { name: 'copy_as_new_version', label: 'Copy as new version' }; |
| 12 | + |
| 13 | +describe('ActionSchema.onSuccess (#9566/#9474)', () => { |
| 14 | + describe('accept pins', () => { |
| 15 | + it('accepts the full shape on a type:api action', () => { |
| 16 | + const r = ActionSchema.safeParse({ |
| 17 | + ...base, |
| 18 | + type: 'api', |
| 19 | + target: '/api/v1/actions/task_version/clone', |
| 20 | + onSuccess: { navigate: '/apps/mfg/task_version/${result.id}', openIn: 'newTab' }, |
| 21 | + }); |
| 22 | + expect(r.success, JSON.stringify((r as { error?: unknown }).error)).toBe(true); |
| 23 | + expect((r.data as { onSuccess: unknown }).onSuccess) |
| 24 | + .toEqual({ navigate: '/apps/mfg/task_version/${result.id}', openIn: 'newTab' }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('accepts the minimal shape on a type:script action', () => { |
| 28 | + const r = ActionSchema.safeParse({ |
| 29 | + ...base, |
| 30 | + type: 'script', |
| 31 | + target: 'cloneVersion', |
| 32 | + onSuccess: { navigate: '/apps/mfg/task_version/${result.id}' }, |
| 33 | + }); |
| 34 | + expect(r.success, JSON.stringify((r as { error?: unknown }).error)).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('reaches the same shape through the registered `action` metadata schema (the parsing door)', () => { |
| 38 | + const schema = getMetadataTypeSchema('action'); |
| 39 | + expect(schema).toBeDefined(); |
| 40 | + const r = schema!.safeParse({ |
| 41 | + ...base, |
| 42 | + type: 'api', |
| 43 | + target: '/api/v1/actions/task_version/clone', |
| 44 | + onSuccess: { navigate: '/apps/mfg/task_version/${result.id}' }, |
| 45 | + }); |
| 46 | + expect(r.success, JSON.stringify((r as { error?: unknown }).error)).toBe(true); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('default pin — openIn materializes to self', () => { |
| 51 | + it('parse output carries openIn "self" when the author omits it', () => { |
| 52 | + // The ruled default is MATERIALIZED (`.default('self')`), so a consumer |
| 53 | + // reads the resolved member off the parse output and never needs its own |
| 54 | + // fallback — declared = enforced. This is the observable being pinned. |
| 55 | + const out = ActionSchema.parse({ |
| 56 | + ...base, |
| 57 | + type: 'api', |
| 58 | + target: '/api/v1/actions/task_version/clone', |
| 59 | + onSuccess: { navigate: '/x/${result.id}' }, |
| 60 | + }) as { onSuccess?: { navigate: string; openIn: string } }; |
| 61 | + expect(out.onSuccess?.openIn).toBe('self'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('an explicit openIn survives untouched', () => { |
| 65 | + const out = ActionSchema.parse({ |
| 66 | + ...base, |
| 67 | + type: 'script', |
| 68 | + target: 'cloneVersion', |
| 69 | + onSuccess: { navigate: '/x', openIn: 'newTab' }, |
| 70 | + }) as { onSuccess?: { openIn: string } }; |
| 71 | + expect(out.onSuccess?.openIn).toBe('newTab'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('refusal pins — the closed enum', () => { |
| 76 | + it('rejects an out-of-vocabulary openIn', () => { |
| 77 | + const r = ActionSchema.safeParse({ |
| 78 | + ...base, |
| 79 | + type: 'api', |
| 80 | + target: '/t', |
| 81 | + onSuccess: { navigate: '/x', openIn: 'modal' }, |
| 82 | + }); |
| 83 | + expect(r.success).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it("names the camelCase member when the author writes the sibling key's kebab spelling", () => { |
| 87 | + // The top-level `openIn` (type:'url') spells its member 'new-tab'; the |
| 88 | + // handler-return convention and this key spell it 'newTab'. The enum's |
| 89 | + // error map catches exactly the crossover spelling (the |
| 90 | + // ActionLocationSchema issue.input precedent) — every other wrong value |
| 91 | + // keeps zod's own enum error. |
| 92 | + const r = ActionSchema.safeParse({ |
| 93 | + ...base, |
| 94 | + type: 'api', |
| 95 | + target: '/t', |
| 96 | + onSuccess: { navigate: '/x', openIn: 'new-tab' }, |
| 97 | + }); |
| 98 | + expect(r.success).toBe(false); |
| 99 | + const msg = r.error!.issues.map((i) => i.message).join('\n'); |
| 100 | + expect(msg).toContain("'newTab'"); |
| 101 | + expect(msg).toContain('new-tab'); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('refusal pins — the strict inner object', () => { |
| 106 | + const innerIssue = (onSuccess: Record<string, unknown>) => { |
| 107 | + const r = ActionSchema.safeParse({ ...base, type: 'api', target: '/t', onSuccess }); |
| 108 | + expect(r.success).toBe(false); |
| 109 | + return r.error!.issues.find((i) => i.code === 'unrecognized_keys'); |
| 110 | + }; |
| 111 | + |
| 112 | + it('rejects an undeclared key instead of silently dropping it', () => { |
| 113 | + const issue = innerIssue({ navigate: '/x', notAKey: 1 }); |
| 114 | + expect(issue).toBeDefined(); |
| 115 | + expect(issue!.message).toContain('`notAKey`'); |
| 116 | + }); |
| 117 | + |
| 118 | + it("points the handler-return spelling `redirectUrl` at `navigate`", () => { |
| 119 | + expect(innerIssue({ redirectUrl: '/x' })!.message) |
| 120 | + .toContain('`redirectUrl` → `navigate`'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('points the generic destination spellings at `navigate`', () => { |
| 124 | + for (const key of ['url', 'to', 'route', 'path', 'target']) { |
| 125 | + expect(innerIssue({ [key]: '/x' })!.message) |
| 126 | + .toContain(`\`${key}\` → \`navigate\``); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + it('tells an author reaching for `opensInNewTab` that the tab choice here is openIn', () => { |
| 131 | + expect(innerIssue({ navigate: '/x', opensInNewTab: true })!.message) |
| 132 | + .toContain("openIn: 'newTab'"); |
| 133 | + }); |
| 134 | + |
| 135 | + it('requires `navigate` — an empty onSuccess block is not a declaration', () => { |
| 136 | + const r = ActionSchema.safeParse({ ...base, type: 'api', target: '/t', onSuccess: {} }); |
| 137 | + expect(r.success).toBe(false); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('type scope — api and script only (the #4352 enforcement shape)', () => { |
| 142 | + it.each(['url', 'modal', 'flow', 'form'] as const)('refuses onSuccess on a type:%s action', (type) => { |
| 143 | + const r = ActionSchema.safeParse({ |
| 144 | + ...base, |
| 145 | + type, |
| 146 | + target: type === 'form' ? 'edit_form' : '/t', |
| 147 | + onSuccess: { navigate: '/x' }, |
| 148 | + }); |
| 149 | + expect(r.success).toBe(false); |
| 150 | + const msg = r.error!.issues.map((i) => i.message).join('\n'); |
| 151 | + expect(msg).toContain('onSuccess'); |
| 152 | + expect(msg).toContain("'api'"); |
| 153 | + expect(msg).toContain("'script'"); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('the pre-existing probes now land on prescriptions, not bare rejections (#9474)', () => { |
| 158 | + it('a top-level `redirect` names the onSuccess shape', () => { |
| 159 | + const r = ActionSchema.safeParse({ ...base, type: 'api', target: '/t', redirect: '/x' }); |
| 160 | + expect(r.success).toBe(false); |
| 161 | + const issue = r.error!.issues.find((i) => i.code === 'unrecognized_keys'); |
| 162 | + expect(issue!.message).toContain('onSuccess'); |
| 163 | + expect(issue!.message).toContain('navigate'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('a top-level `redirectUrl` says it is the handler-return convention, not an authorable key', () => { |
| 167 | + const r = ActionSchema.safeParse({ ...base, type: 'api', target: '/t', redirectUrl: '/x' }); |
| 168 | + expect(r.success).toBe(false); |
| 169 | + const issue = r.error!.issues.find((i) => i.code === 'unrecognized_keys'); |
| 170 | + expect(issue!.message).toContain('HANDLER-RETURN'); |
| 171 | + expect(issue!.message).toContain('onSuccess'); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + describe('strictness posture unchanged elsewhere', () => { |
| 176 | + it('InlineActionSchema does not pick onSuccess — unknown key inline', () => { |
| 177 | + // The inline surface widens when a renderer widens (the file's own rule); |
| 178 | + // element:button's forward list has no onSuccess hop, so the key is |
| 179 | + // registered-actions-only until the objectui half lands. |
| 180 | + const r = InlineActionSchema.safeParse({ |
| 181 | + type: 'api', |
| 182 | + target: '/t', |
| 183 | + onSuccess: { navigate: '/x' }, |
| 184 | + }); |
| 185 | + expect(r.success).toBe(false); |
| 186 | + }); |
| 187 | + |
| 188 | + it('an action WITHOUT onSuccess still parses exactly as before', () => { |
| 189 | + const out = ActionSchema.parse({ ...base, type: 'api', target: '/t' }) as Record<string, unknown>; |
| 190 | + expect('onSuccess' in out && out.onSuccess !== undefined).toBe(false); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments