|
| 1 | +// oxlint-disable max-params -- Api cores include the dispatch node before method arguments. |
| 2 | +import { describe, expect, test } from 'vitest'; |
| 3 | +import { createApi } from './api.ts'; |
| 4 | +import { createNode } from './node.ts'; |
| 5 | +import type { Node } from './node.ts'; |
| 6 | + |
| 7 | +describe('createApi', () => { |
| 8 | + test('methods call a core with the node as its first argument', () => { |
| 9 | + const api = createApi('test', { |
| 10 | + add: (_node: Node, a: number, b: number) => a + b, |
| 11 | + }); |
| 12 | + expect(api.methods.add(createNode(), 2, 3)).toBe(5); |
| 13 | + }); |
| 14 | + |
| 15 | + test('a core receives the node it is invoked on', () => { |
| 16 | + const api = createApi('test', { |
| 17 | + self: (node: Node) => node, |
| 18 | + }); |
| 19 | + const n = createNode(); |
| 20 | + expect(api.methods.self(n)).toBe(n); |
| 21 | + }); |
| 22 | + |
| 23 | + test('invoke calls a core using the first argument as the node', () => { |
| 24 | + const api = createApi('test', { |
| 25 | + add: (_node: Node, a: number, b: number) => a + b, |
| 26 | + }); |
| 27 | + expect(api.invoke('add', [createNode(), 2, 3])).toBe(5); |
| 28 | + }); |
| 29 | + |
| 30 | + test('invokes the core directly when no middleware is installed', () => { |
| 31 | + const api = createApi('test', { |
| 32 | + num: (_node: Node, value: number) => value, |
| 33 | + }); |
| 34 | + expect(api.methods.num(createNode(), 7)).toBe(7); |
| 35 | + }); |
| 36 | + |
| 37 | + test('middleware surrounds a core function', () => { |
| 38 | + const api = createApi('test', { |
| 39 | + num: (_node: Node, value: number) => value, |
| 40 | + }); |
| 41 | + const n = createNode(); |
| 42 | + api.around(n, { |
| 43 | + num: (args, next) => next(...args) * 2, |
| 44 | + }); |
| 45 | + expect(api.methods.num(n, 5)).toBe(10); |
| 46 | + }); |
| 47 | + |
| 48 | + test('middleware receives the invocation node as its first argument', () => { |
| 49 | + const api = createApi('test', { |
| 50 | + num: (_node: Node, value: number) => value, |
| 51 | + }); |
| 52 | + const n = createNode(); |
| 53 | + let seen: Node | undefined; |
| 54 | + api.around(n, { |
| 55 | + num: (args, next) => { |
| 56 | + seen = args[0]; |
| 57 | + return next(...args); |
| 58 | + }, |
| 59 | + }); |
| 60 | + api.methods.num(n, 1); |
| 61 | + expect(seen).toBe(n); |
| 62 | + }); |
| 63 | + |
| 64 | + test('middleware can substitute the node passed on to the core', () => { |
| 65 | + const api = createApi('test', { |
| 66 | + self: (node: Node) => node, |
| 67 | + }); |
| 68 | + const root = createNode(); |
| 69 | + const target = createNode(root); |
| 70 | + api.around(root, { |
| 71 | + self: (_args, next) => next(target), |
| 72 | + }); |
| 73 | + expect(api.methods.self(root)).toBe(target); |
| 74 | + }); |
| 75 | + |
| 76 | + test('a child node inherits middleware from its parent', () => { |
| 77 | + const api = createApi('test', { |
| 78 | + num: (_node: Node, value: number) => value, |
| 79 | + }); |
| 80 | + const parent = createNode(); |
| 81 | + api.around(parent, { |
| 82 | + num: (args, next) => next(...args) * 2, |
| 83 | + }); |
| 84 | + expect(api.methods.num(createNode(parent), 5)).toBe(10); |
| 85 | + }); |
| 86 | + |
| 87 | + test('composes middleware on a node in the order it was installed', () => { |
| 88 | + const api = createApi('test', { |
| 89 | + seq: (_node: Node, order: string[]) => order, |
| 90 | + }); |
| 91 | + const n = createNode(); |
| 92 | + api.around(n, { |
| 93 | + seq: (args, next) => next(args[0], args[1].concat('one')).concat('/one'), |
| 94 | + }); |
| 95 | + api.around(n, { |
| 96 | + seq: (args, next) => next(args[0], args[1].concat('two')).concat('/two'), |
| 97 | + }); |
| 98 | + api.around(n, { |
| 99 | + seq: (args, next) => next(args[0], args[1].concat('three')).concat('/three'), |
| 100 | + }); |
| 101 | + expect(api.methods.seq(n, [])).toEqual(['one', 'two', 'three', '/three', '/two', '/one']); |
| 102 | + }); |
| 103 | + |
| 104 | + test('middleware on one key does not affect another key', () => { |
| 105 | + const api = createApi('test', { |
| 106 | + foo: (_node: Node) => 1, |
| 107 | + bar: (_node: Node) => 1, |
| 108 | + }); |
| 109 | + const n = createNode(); |
| 110 | + api.around(n, { |
| 111 | + foo: (args, next) => next(...args) * 100, |
| 112 | + }); |
| 113 | + expect(api.methods.foo(n)).toBe(100); |
| 114 | + expect(api.methods.bar(n)).toBe(1); |
| 115 | + }); |
| 116 | + |
| 117 | + test('sibling nodes are isolated from each other’s middleware', () => { |
| 118 | + const api = createApi('test', { |
| 119 | + seq: (_node: Node, order: string[]) => order, |
| 120 | + }); |
| 121 | + const root = createNode(); |
| 122 | + const a = createNode(root); |
| 123 | + api.around(a, { |
| 124 | + seq: (args, next) => next(args[0], args[1].concat('a')).concat('/a'), |
| 125 | + }); |
| 126 | + const b = createNode(root); |
| 127 | + api.around(b, { |
| 128 | + seq: (args, next) => next(args[0], args[1].concat('b')).concat('/b'), |
| 129 | + }); |
| 130 | + expect(api.methods.seq(a, [])).toEqual(['a', '/a']); |
| 131 | + expect(api.methods.seq(b, [])).toEqual(['b', '/b']); |
| 132 | + expect(api.methods.seq(root, [])).toEqual([]); |
| 133 | + }); |
| 134 | + |
| 135 | + test('composes middleware across a three-level node tree, ancestor outermost', () => { |
| 136 | + const api = createApi('test', { |
| 137 | + seq: (_node: Node, order: string[]) => order, |
| 138 | + }); |
| 139 | + const grand = createNode(); |
| 140 | + api.around(grand, { |
| 141 | + seq: (args, next) => next(args[0], args[1].concat('grand')).concat('/grand'), |
| 142 | + }); |
| 143 | + const parent = createNode(grand); |
| 144 | + api.around(parent, { |
| 145 | + seq: (args, next) => next(args[0], args[1].concat('parent')).concat('/parent'), |
| 146 | + }); |
| 147 | + const child = createNode(parent); |
| 148 | + api.around(child, { |
| 149 | + seq: (args, next) => next(args[0], args[1].concat('child')).concat('/child'), |
| 150 | + }); |
| 151 | + expect(api.methods.seq(child, [])).toEqual([ |
| 152 | + 'grand', |
| 153 | + 'parent', |
| 154 | + 'child', |
| 155 | + '/child', |
| 156 | + '/parent', |
| 157 | + '/grand', |
| 158 | + ]); |
| 159 | + }); |
| 160 | + |
| 161 | + test('does not duplicate ancestor middleware when an ancestor is extended later', () => { |
| 162 | + const api = createApi('test', { |
| 163 | + seq: (_node: Node, order: string[]) => order, |
| 164 | + }); |
| 165 | + const parent = createNode(); |
| 166 | + const child = createNode(parent); |
| 167 | + parent.children.add(child); |
| 168 | + |
| 169 | + api.around(parent, { |
| 170 | + seq: (args, next) => next(args[0], args[1].concat('parent-one')), |
| 171 | + }); |
| 172 | + api.around(child, { |
| 173 | + seq: (args, next) => next(args[0], args[1].concat('child')), |
| 174 | + }); |
| 175 | + api.around(parent, { |
| 176 | + seq: (args, next) => next(args[0], args[1].concat('parent-two')), |
| 177 | + }); |
| 178 | + |
| 179 | + expect(api.methods.seq(child, [])).toEqual(['parent-one', 'parent-two', 'child']); |
| 180 | + }); |
| 181 | + |
| 182 | + test('a child’s middleware does not leak back to its parent', () => { |
| 183 | + const api = createApi('test', { |
| 184 | + num: (_node: Node, value: number) => value, |
| 185 | + }); |
| 186 | + const parent = createNode(); |
| 187 | + const child = createNode(parent); |
| 188 | + api.around(child, { |
| 189 | + num: (args, next) => next(...args) * 2, |
| 190 | + }); |
| 191 | + expect(api.methods.num(child, 5)).toBe(10); |
| 192 | + expect(api.methods.num(parent, 5)).toBe(5); |
| 193 | + }); |
| 194 | +}); |
0 commit comments