|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * The `mcp:authorized-assistants` arm (`../zod/mcp-authorized-assistants.zod.ts`). |
| 11 | + * |
| 12 | + * `@object-ui/app-shell` registers `mcp:authorized-assistants` for a `sys_user` |
| 13 | + * record page, and this arm lands with it so `AnyComponentSchema` claims the |
| 14 | + * type instead of refusing it at `type`. The arm is declared from the widget's |
| 15 | + * read points: the widget reads no prop (the person is the bound record), so |
| 16 | + * the only `properties` bag it takes is `{}`, and both content channels are |
| 17 | + * refused by name. |
| 18 | + * |
| 19 | + * Refusals are asserted by issue `code` and `path` — the envelope a consumer |
| 20 | + * reads — and not by message wording. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, expect, it } from 'vitest'; |
| 24 | + |
| 25 | +import { |
| 26 | + AnyComponentSchema, |
| 27 | + McpAuthorizedAssistantsSchema, |
| 28 | + StrictAnyComponentSchema, |
| 29 | + safeValidateSchema, |
| 30 | +} from '../zod/index.zod.js'; |
| 31 | + |
| 32 | +const TYPE = 'mcp:authorized-assistants'; |
| 33 | + |
| 34 | +/** The issues of a refused parse, as `code` + dotted `path`. */ |
| 35 | +function issuesOf(document: unknown): { code: string; path: string; message: string }[] { |
| 36 | + const result = safeValidateSchema(document); |
| 37 | + expect(result.success, 'expected a refusal').toBe(false); |
| 38 | + if (result.success) return []; |
| 39 | + return result.error.issues.map((issue) => ({ |
| 40 | + code: issue.code, |
| 41 | + path: issue.path.join('.'), |
| 42 | + message: issue.message, |
| 43 | + })); |
| 44 | +} |
| 45 | + |
| 46 | +describe('`mcp:authorized-assistants` — the node a `sys_user` record page authors', () => { |
| 47 | + it('accepts the bare node on every face', () => { |
| 48 | + const node = { type: TYPE }; |
| 49 | + expect(McpAuthorizedAssistantsSchema.safeParse(node).success).toBe(true); |
| 50 | + expect(safeValidateSchema(node).success).toBe(true); |
| 51 | + expect(StrictAnyComponentSchema.safeParse(node).success).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('accepts an empty `properties` bag on every face', () => { |
| 55 | + const node = { type: TYPE, properties: {} }; |
| 56 | + expect(McpAuthorizedAssistantsSchema.safeParse(node).success).toBe(true); |
| 57 | + expect(safeValidateSchema(node).success).toBe(true); |
| 58 | + expect(StrictAnyComponentSchema.safeParse(node).success).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('accepts the node inside a page, with the spec page-component keys a page writes beside it', () => { |
| 62 | + const page = { |
| 63 | + type: 'page', |
| 64 | + children: [{ type: TYPE, id: 'user_authorized_assistants', className: 'mt-2' }], |
| 65 | + }; |
| 66 | + expect(safeValidateSchema(page).success).toBe(true); |
| 67 | + expect(StrictAnyComponentSchema.safeParse(page).success).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it('is claimed at `type` — the union no longer refuses the literal', () => { |
| 71 | + // The ratchet's reading (`registered-types-validate-ratchet-10859.test.ts`). |
| 72 | + expect(safeValidateSchema({ type: TYPE }).success).toBe(true); |
| 73 | + // Control: an unclaimed `mcp:` literal IS refused there. |
| 74 | + const unclaimed = issuesOf({ type: 'mcp:no-such-widget' }); |
| 75 | + expect(unclaimed.some((i) => i.code === 'invalid_union' && i.path === 'type')).toBe(true); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('`mcp:authorized-assistants` — what the arm refuses', () => { |
| 80 | + it('refuses any key inside the bag — the widget reads none, the person included', () => { |
| 81 | + expect(issuesOf({ type: TYPE, properties: { userId: 'user_mia' } })).toEqual([ |
| 82 | + expect.objectContaining({ code: 'unrecognized_keys', path: 'properties' }), |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('refuses a bag that is not an object', () => { |
| 87 | + expect(issuesOf({ type: TYPE, properties: 'user_mia' })).toEqual([ |
| 88 | + expect.objectContaining({ code: 'invalid_type', path: 'properties' }), |
| 89 | + ]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('refuses a flat key the widget does not read, on the strict face', () => { |
| 93 | + const strict = StrictAnyComponentSchema.safeParse({ type: TYPE, userId: 'user_mia' }); |
| 94 | + expect(strict.success).toBe(false); |
| 95 | + expect(strict.error?.issues.map((i) => i.code)).toContain('unrecognized_keys'); |
| 96 | + }); |
| 97 | + |
| 98 | + it.each(['children', 'body'])('refuses `%s` by name — the widget reads neither content channel', (key) => { |
| 99 | + const issues = issuesOf({ type: TYPE, [key]: [{ type: 'text', content: 'x' }] }); |
| 100 | + expect(issues).toEqual([expect.objectContaining({ code: 'invalid_type', path: key })]); |
| 101 | + expect(issues[0].message).toContain(TYPE); |
| 102 | + }); |
| 103 | + |
| 104 | + it('reaches the arm through the union, not a second schema', () => { |
| 105 | + // The union dispatches on the literal to this arm: the same refusal either way. |
| 106 | + const document = { type: TYPE, properties: { userId: 'user_mia' } }; |
| 107 | + const bare = McpAuthorizedAssistantsSchema.safeParse(document); |
| 108 | + const viaUnion = AnyComponentSchema.safeParse(document); |
| 109 | + expect(bare.success).toBe(false); |
| 110 | + expect(viaUnion.success).toBe(false); |
| 111 | + expect(viaUnion.error?.issues.map((i) => [i.code, i.path.join('.')])).toEqual( |
| 112 | + bare.error?.issues.map((i) => [i.code, i.path.join('.')]), |
| 113 | + ); |
| 114 | + }); |
| 115 | +}); |
0 commit comments