Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/utils.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586import { describe, it } from 'node:test';import { expect } from '@std/expect';
import * as catalog from './mod.ts';
const First = Object.freeze({ id: 'test:first', kind: 'test', description: 'First entry.' });const Second = Object.freeze({ id: 'test:second', kind: 'test' });
describe('catalog', () => { it('preserves prototype-shaped keys as ordinary entries', () => { const entries = Object.create(null) as Record<string, typeof First>; Object.defineProperty(entries, '__proto__', { value: First, enumerable: true }); Object.defineProperty(entries, 'constructor', { value: Second, enumerable: true }); const definitions = catalog.create('test', entries);
expect(definitions.__proto__).toBe(First); expect(definitions.constructor).toBe(Second); expect(catalog.values(definitions)).toEqual([First, Second]); });
it('deduplicates the same object and rejects conflicting stable ids', () => { expect(catalog.compose(First, [First, Second])).toEqual([First, Second]); expect(() => catalog.compose(First, { ...First })).toThrow(catalog.CatalogConflictError); });
it('keeps selection keys and direct definition identities', () => { const definitions = catalog.create('test', { First, Second }); const selected = catalog.select(definitions, ['Second', 'First'] as const);
expect(selected.Second).toBe(Second); expect(selected.First).toBe(First); expect(catalog.values(selected)).toEqual([Second, First]); expect(catalog.document(selected)).toEqual({ type: 'selection', namespace: 'test', entries: [ { key: 'Second', id: 'test:second', kind: 'test' }, { key: 'First', id: 'test:first', kind: 'test', description: 'First entry.' }, ], }); });
it('rejects assigning one definition object to multiple source keys', () => { expect(() => catalog.create('test', { First, Alias: First })).toThrow(TypeError); });
it('does not expose mutable catalog metadata indexes', () => { const definitions = catalog.create('test', { First, Second }); const keyByEntry = catalog.metadata(definitions).keyByEntry as ReadonlyMap<typeof First | typeof Second, string> & { set?: unknown; };
expect(keyByEntry.get(First)).toBe('First'); expect(keyByEntry.set).toBe(undefined); });
it('walks deeply nested composition without recursive stack growth', () => { let nested: catalog.DefinitionInput<typeof First> = First; for (let depth = 0; depth < 25_000; depth++) nested = [nested];
const composed = catalog.compose(nested);
expect(composed).toEqual([First]); });
it('freezes the catalog and selection without adding enumerable methods', () => { const definitions = catalog.create('test', { First, Second }); const selected = catalog.select(definitions, ['First'] as const);
expect(Object.keys(definitions)).toEqual(['First', 'Second']); expect(Object.keys(selected)).toEqual(['First']); expect(Object.isFrozen(definitions)).toBe(true); expect(Object.isFrozen(selected)).toBe(true); });
it('keeps catalog identity private and rejects lookalike records', () => { const definitions = catalog.create('test-private', { First, Second }); const lookalike = Object.freeze({ First, Second });
expect(catalog.isRoot(definitions)).toBe(true); expect(catalog.isRoot(lookalike)).toBe(false); expect(() => catalog.metadata(lookalike as never)).toThrow(TypeError); });});