|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `normalizeMetadataCollection` reads ONLY a plain object as the map form. |
| 5 | + * |
| 6 | + * ## What was wrong |
| 7 | + * |
| 8 | + * The map-form branch tested `typeof value === 'object'`, so a `Set`, a `Map` |
| 9 | + * or a `Date` was read as a map too. `Object.entries` of any of them is `[]`, |
| 10 | + * and normalization runs before the schema parse, so the strict `defineStack` |
| 11 | + * door saw a valid empty array and ACCEPTED the stack with every authored |
| 12 | + * entry (e.g. its permission-set grants) gone. |
| 13 | + * |
| 14 | + * ## What is pinned |
| 15 | + * |
| 16 | + * A composed artifact is complete or it is refused. For every key that accepts |
| 17 | + * the map form (derived from `MAP_SUPPORTED_FIELDS`, never transcribed), a |
| 18 | + * non-plain object reaches the strict parse unchanged and is refused with the |
| 19 | + * ordinary strict envelope: `STACK_SCHEMA_INVALID`, `status: 422`, a zod issue |
| 20 | + * rooted at the key expecting an array. The controls: the same key authored as |
| 21 | + * a plain-object map — a literal, a null-prototype object, and a plain object |
| 22 | + * from another realm — is still normalized. |
| 23 | + */ |
| 24 | +import { describe, it, expect } from 'vitest'; |
| 25 | +import { runInNewContext } from 'node:vm'; |
| 26 | +import { normalizeMetadataCollection, MAP_SUPPORTED_FIELDS } from './metadata-collection.zod'; |
| 27 | +import { defineStack } from '../stack.zod'; |
| 28 | + |
| 29 | +type Envelope = Error & { |
| 30 | + code?: string; |
| 31 | + status?: number; |
| 32 | + issues?: ReadonlyArray<{ code?: string; path?: readonly PropertyKey[]; expected?: string }>; |
| 33 | +}; |
| 34 | + |
| 35 | +/** The thrown value, or `null` when the stack is accepted. */ |
| 36 | +function refusal(fn: () => unknown): Envelope | null { |
| 37 | + try { |
| 38 | + fn(); |
| 39 | + return null; |
| 40 | + } catch (e) { |
| 41 | + return e as Envelope; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +const manifest = { id: 'com.example.a', name: 'a', version: '1.0.0', type: 'app' as const }; |
| 46 | + |
| 47 | +class Holder { |
| 48 | + rep = { label: 'Rep' }; |
| 49 | +} |
| 50 | + |
| 51 | +const NON_PLAIN: ReadonlyArray<readonly [string, () => unknown]> = [ |
| 52 | + ['a Set', () => new Set([{ name: 'rep', label: 'Rep' }])], |
| 53 | + ['a Map', () => new Map([['rep', { label: 'Rep' }]])], |
| 54 | + ['a Date', () => new Date(0)], |
| 55 | + ['a class instance', () => new Holder()], |
| 56 | +]; |
| 57 | + |
| 58 | +describe('normalizeMetadataCollection — only a plain object is the map form', () => { |
| 59 | + for (const [label, make] of NON_PLAIN) { |
| 60 | + it(`${label} is returned unchanged, never read as an empty map`, () => { |
| 61 | + const value = make(); |
| 62 | + expect(normalizeMetadataCollection(value)).toBe(value); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + it('control: an object literal is normalized with key → name', () => { |
| 67 | + expect(normalizeMetadataCollection({ rep: { label: 'Rep' } })).toEqual([{ name: 'rep', label: 'Rep' }]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('control: a null-prototype object is normalized with key → name', () => { |
| 71 | + const map = Object.assign(Object.create(null), { rep: { label: 'Rep' } }); |
| 72 | + expect(normalizeMetadataCollection(map)).toEqual([{ name: 'rep', label: 'Rep' }]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('control: a plain object from another realm is normalized with key → name', () => { |
| 76 | + const map = runInNewContext('({ rep: { label: "Rep" } })'); |
| 77 | + expect(Object.getPrototypeOf(map)).not.toBe(Object.prototype); |
| 78 | + expect(normalizeMetadataCollection(map)).toEqual([{ name: 'rep', label: 'Rep' }]); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('strict defineStack refuses a non-plain object for a map-form collection key', () => { |
| 83 | + it('covers every map-form key the normalizer declares (the list is the census)', () => { |
| 84 | + expect(MAP_SUPPORTED_FIELDS).toContain('permissions'); |
| 85 | + expect(MAP_SUPPORTED_FIELDS.length).toBeGreaterThanOrEqual(20); |
| 86 | + }); |
| 87 | + |
| 88 | + for (const key of MAP_SUPPORTED_FIELDS) { |
| 89 | + for (const [label, make] of NON_PLAIN) { |
| 90 | + it(`'${key}': ${label} is refused with STACK_SCHEMA_INVALID / 422 at the key — never accepted as []`, () => { |
| 91 | + const err = refusal(() => defineStack({ manifest, [key]: make() } as never)); |
| 92 | + expect(err, `'${key}' given ${label} was accepted`).not.toBeNull(); |
| 93 | + expect(err!.code).toBe('STACK_SCHEMA_INVALID'); |
| 94 | + expect(err!.status).toBe(422); |
| 95 | + expect(err!.issues).toEqual( |
| 96 | + expect.arrayContaining([ |
| 97 | + expect.objectContaining({ code: 'invalid_type', path: [key], expected: 'array' }), |
| 98 | + ]), |
| 99 | + ); |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + it("control: 'permissions' authored as a plain-object map is accepted with its entry", () => { |
| 105 | + const stack = defineStack({ |
| 106 | + manifest, |
| 107 | + permissions: { rep: { label: 'Rep', objects: {} } }, |
| 108 | + } as never); |
| 109 | + expect(stack.permissions?.map((p) => p.name)).toEqual(['rep']); |
| 110 | + }); |
| 111 | +}); |
0 commit comments