|
| 1 | +import { makeExo } from '@endo/exo'; |
| 2 | +import { |
| 3 | + M, |
| 4 | + matches, |
| 5 | + getInterfaceGuardPayload, |
| 6 | + getMethodGuardPayload, |
| 7 | +} from '@endo/patterns'; |
| 8 | +import type { MethodGuard, Pattern } from '@endo/patterns'; |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | + |
| 11 | +import { collectSheafGuard } from './guard.ts'; |
| 12 | +import type { Section } from './types.ts'; |
| 13 | + |
| 14 | +const makeSection = ( |
| 15 | + tag: string, |
| 16 | + guards: Record<string, MethodGuard>, |
| 17 | + methods: Record<string, (...args: unknown[]) => unknown>, |
| 18 | +): Section => { |
| 19 | + const interfaceGuard = M.interface(tag, guards); |
| 20 | + return makeExo(tag, interfaceGuard, methods) as unknown as Section; |
| 21 | +}; |
| 22 | + |
| 23 | +describe('collectSheafGuard', () => { |
| 24 | + it('variable arity: add with 1, 2, and 3 args', () => { |
| 25 | + const sections = [ |
| 26 | + makeSection( |
| 27 | + 'Calc:0', |
| 28 | + { add: M.call(M.number()).returns(M.number()) }, |
| 29 | + { add: (a: number) => a }, |
| 30 | + ), |
| 31 | + makeSection( |
| 32 | + 'Calc:1', |
| 33 | + { add: M.call(M.number(), M.number()).returns(M.number()) }, |
| 34 | + { add: (a: number, b: number) => a + b }, |
| 35 | + ), |
| 36 | + makeSection( |
| 37 | + 'Calc:2', |
| 38 | + { |
| 39 | + add: M.call(M.number(), M.number(), M.number()).returns(M.number()), |
| 40 | + }, |
| 41 | + { add: (a: number, b: number, cc: number) => a + b + cc }, |
| 42 | + ), |
| 43 | + ]; |
| 44 | + |
| 45 | + const guard = collectSheafGuard('Calc', sections); |
| 46 | + const { methodGuards } = getInterfaceGuardPayload(guard) as unknown as { |
| 47 | + methodGuards: Record<string, MethodGuard>; |
| 48 | + }; |
| 49 | + const payload = getMethodGuardPayload(methodGuards.add) as unknown as { |
| 50 | + argGuards: Pattern[]; |
| 51 | + optionalArgGuards?: Pattern[]; |
| 52 | + }; |
| 53 | + |
| 54 | + // 1 required arg (present in all), 2 optional (variable arity) |
| 55 | + expect(payload.argGuards).toHaveLength(1); |
| 56 | + expect(payload.optionalArgGuards).toHaveLength(2); |
| 57 | + }); |
| 58 | + |
| 59 | + it('return guard union', () => { |
| 60 | + const sections = [ |
| 61 | + makeSection( |
| 62 | + 'S:0', |
| 63 | + { f: M.call(M.eq(0)).returns(M.eq(0)) }, |
| 64 | + { f: (_: number) => 0 }, |
| 65 | + ), |
| 66 | + makeSection( |
| 67 | + 'S:1', |
| 68 | + { f: M.call(M.eq(1)).returns(M.eq(1)) }, |
| 69 | + { f: (_: number) => 1 }, |
| 70 | + ), |
| 71 | + ]; |
| 72 | + |
| 73 | + const guard = collectSheafGuard('S', sections); |
| 74 | + const { methodGuards } = getInterfaceGuardPayload(guard) as unknown as { |
| 75 | + methodGuards: Record<string, MethodGuard>; |
| 76 | + }; |
| 77 | + const { returnGuard } = getMethodGuardPayload( |
| 78 | + methodGuards.f, |
| 79 | + ) as unknown as { returnGuard: Pattern }; |
| 80 | + |
| 81 | + // Return guard is union of eq(0) and eq(1) |
| 82 | + expect(matches(0, returnGuard)).toBe(true); |
| 83 | + expect(matches(1, returnGuard)).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it('section with its own optional args: optional preserved in union', () => { |
| 87 | + const sections = [ |
| 88 | + makeSection( |
| 89 | + 'Greeter', |
| 90 | + { |
| 91 | + greet: M.callWhen(M.string()) |
| 92 | + .optional(M.string()) |
| 93 | + .returns(M.string()), |
| 94 | + }, |
| 95 | + { greet: (name: string, _greeting?: string) => `hello ${name}` }, |
| 96 | + ), |
| 97 | + ]; |
| 98 | + |
| 99 | + const guard = collectSheafGuard('Greeter', sections); |
| 100 | + const { methodGuards } = getInterfaceGuardPayload(guard) as unknown as { |
| 101 | + methodGuards: Record<string, MethodGuard>; |
| 102 | + }; |
| 103 | + const payload = getMethodGuardPayload(methodGuards.greet) as unknown as { |
| 104 | + argGuards: Pattern[]; |
| 105 | + optionalArgGuards?: Pattern[]; |
| 106 | + }; |
| 107 | + |
| 108 | + expect(payload.argGuards).toHaveLength(1); |
| 109 | + expect(payload.optionalArgGuards).toHaveLength(1); |
| 110 | + }); |
| 111 | + |
| 112 | + it('rest arg guard preserved in collected union', () => { |
| 113 | + const sections = [ |
| 114 | + makeSection( |
| 115 | + 'Logger', |
| 116 | + { log: M.call(M.string()).rest(M.string()).returns(M.any()) }, |
| 117 | + { log: (..._args: string[]) => undefined }, |
| 118 | + ), |
| 119 | + ]; |
| 120 | + |
| 121 | + const guard = collectSheafGuard('Logger', sections); |
| 122 | + const { methodGuards } = getInterfaceGuardPayload(guard) as unknown as { |
| 123 | + methodGuards: Record<string, MethodGuard>; |
| 124 | + }; |
| 125 | + const payload = getMethodGuardPayload(methodGuards.log) as unknown as { |
| 126 | + argGuards: Pattern[]; |
| 127 | + optionalArgGuards?: Pattern[]; |
| 128 | + restArgGuard?: Pattern; |
| 129 | + }; |
| 130 | + |
| 131 | + expect(payload.argGuards).toHaveLength(1); |
| 132 | + expect(payload.optionalArgGuards ?? []).toHaveLength(0); |
| 133 | + expect(payload.restArgGuard).toBeDefined(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('rest arg guards unioned across sections', () => { |
| 137 | + const sections = [ |
| 138 | + makeSection( |
| 139 | + 'A', |
| 140 | + { log: M.call(M.string()).rest(M.string()).returns(M.any()) }, |
| 141 | + { log: (..._args: string[]) => undefined }, |
| 142 | + ), |
| 143 | + makeSection( |
| 144 | + 'B', |
| 145 | + { log: M.call(M.string()).rest(M.number()).returns(M.any()) }, |
| 146 | + { log: (..._args: unknown[]) => undefined }, |
| 147 | + ), |
| 148 | + ]; |
| 149 | + |
| 150 | + const guard = collectSheafGuard('AB', sections); |
| 151 | + const { methodGuards } = getInterfaceGuardPayload(guard) as unknown as { |
| 152 | + methodGuards: Record<string, MethodGuard>; |
| 153 | + }; |
| 154 | + const { restArgGuard } = getMethodGuardPayload( |
| 155 | + methodGuards.log, |
| 156 | + ) as unknown as { restArgGuard?: Pattern }; |
| 157 | + |
| 158 | + expect(matches('hello', restArgGuard)).toBe(true); |
| 159 | + expect(matches(42, restArgGuard)).toBe(true); |
| 160 | + }); |
| 161 | + |
| 162 | + it('multi-method guard collection', () => { |
| 163 | + const sections = [ |
| 164 | + makeSection( |
| 165 | + 'Multi:0', |
| 166 | + { |
| 167 | + translate: M.call(M.string(), M.string()).returns(M.string()), |
| 168 | + }, |
| 169 | + { |
| 170 | + translate: (from: string, to: string) => `${from}->${to}`, |
| 171 | + }, |
| 172 | + ), |
| 173 | + makeSection( |
| 174 | + 'Multi:1', |
| 175 | + { |
| 176 | + translate: M.call(M.string(), M.string()).returns(M.string()), |
| 177 | + summarize: M.call(M.string()).returns(M.string()), |
| 178 | + }, |
| 179 | + { |
| 180 | + translate: (from: string, to: string) => `${from}->${to}`, |
| 181 | + summarize: (text: string) => `summary: ${text}`, |
| 182 | + }, |
| 183 | + ), |
| 184 | + ]; |
| 185 | + |
| 186 | + const guard = collectSheafGuard('Multi', sections); |
| 187 | + const { methodGuards } = getInterfaceGuardPayload(guard) as unknown as { |
| 188 | + methodGuards: Record<string, MethodGuard>; |
| 189 | + }; |
| 190 | + expect('translate' in methodGuards).toBe(true); |
| 191 | + expect('summarize' in methodGuards).toBe(true); |
| 192 | + }); |
| 193 | +}); |
0 commit comments