@openreachtech/hora-skills-ort-renchan 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.ja.md +135 -0
- package/README.md +135 -0
- package/dist/skills/hor-agent-loop/SKILL.md +123 -0
- package/dist/skills/hor-agent-loop/references/core.md +485 -0
- package/dist/skills/hor-agent-loop/references/graphql-integration.md +178 -0
- package/dist/skills/hor-agent-loop/references/renchan-job-integration.md +368 -0
- package/dist/skills/hor-agent-loop/references/testing.md +492 -0
- package/dist/skills/hor-ai-agent-structure/SKILL.md +403 -0
- package/dist/skills/hor-ai-agent-structure/references/loop-and-actions.md +100 -0
- package/dist/skills/hor-ai-prompt-document-store/SKILL.md +220 -0
- package/dist/skills/hor-ai-prompt-document-store/references/models-and-composition.md +156 -0
- package/dist/skills/hor-backend-testing/SKILL.md +118 -0
- package/dist/skills/hor-backend-testing/references/placement-and-ordering.md +89 -0
- package/dist/skills/hor-backend-testing/references/purity-and-mocks.md +82 -0
- package/dist/skills/hor-backend-testing/references/running-tests.md +186 -0
- package/dist/skills/hor-bank-id/SKILL.md +91 -0
- package/dist/skills/hor-build-e2e-test-environment/SKILL.md +609 -0
- package/dist/skills/hor-build-e2e-test-environment/references/compose-definition.md +368 -0
- package/dist/skills/hor-build-e2e-test-environment/references/edge-and-proxy.md +287 -0
- package/dist/skills/hor-build-e2e-test-environment/references/environment-and-ports.md +201 -0
- package/dist/skills/hor-build-e2e-test-environment/references/runner-and-lifecycle.md +330 -0
- package/dist/skills/hor-build-e2e-test-environment/references/seed-data.md +145 -0
- package/dist/skills/hor-build-e2e-test-environment/references/windows-runner.md +146 -0
- package/dist/skills/hor-constant-definition/SKILL.md +139 -0
- package/dist/skills/hor-cookie-authentication/SKILL.md +33 -0
- package/dist/skills/hor-cookie-authentication/references/auth-filter.md +55 -0
- package/dist/skills/hor-cookie-authentication/references/cookie-context.md +53 -0
- package/dist/skills/hor-cookie-authentication/references/dev-login-seeder.md +22 -0
- package/dist/skills/hor-cookie-authentication/references/environment.md +64 -0
- package/dist/skills/hor-cookie-authentication/references/error-codes.md +24 -0
- package/dist/skills/hor-cookie-authentication/references/migrations.md +18 -0
- package/dist/skills/hor-cookie-authentication/references/resolvers.md +97 -0
- package/dist/skills/hor-cookie-authentication/references/session-clerk.md +41 -0
- package/dist/skills/hor-cookie-authentication/references/token-models.md +125 -0
- package/dist/skills/hor-database-design/SKILL.md +54 -0
- package/dist/skills/hor-database-design/references/column-types.md +92 -0
- package/dist/skills/hor-database-design/references/datetimes.md +88 -0
- package/dist/skills/hor-database-design/references/master-tables.md +117 -0
- package/dist/skills/hor-database-design/references/normalization.md +40 -0
- package/dist/skills/hor-database-design/references/update-history.md +35 -0
- package/dist/skills/hor-execution-placement-pattern/SKILL.md +216 -0
- package/dist/skills/hor-external-api-client/SKILL.md +99 -0
- package/dist/skills/hor-external-api-client/references/architecture.md +127 -0
- package/dist/skills/hor-external-api-client/references/authorization.md +105 -0
- package/dist/skills/hor-external-api-client/references/capsule.md +130 -0
- package/dist/skills/hor-external-api-client/references/conventions.md +68 -0
- package/dist/skills/hor-external-api-client/references/launcher.md +138 -0
- package/dist/skills/hor-external-api-client/references/payload.md +176 -0
- package/dist/skills/hor-external-api-client/references/sdk-wrapper.md +291 -0
- package/dist/skills/hor-external-api-client/references/testing.md +130 -0
- package/dist/skills/hor-external-api-client/references/usage.md +152 -0
- package/dist/skills/hor-graphql-schema/SKILL.md +360 -0
- package/dist/skills/hor-graphql-server-engine/SKILL.md +356 -0
- package/dist/skills/hor-light-rag/SKILL.md +242 -0
- package/dist/skills/hor-light-rag/references/retrieval-internals.md +143 -0
- package/dist/skills/hor-multi-llm-provider/SKILL.md +288 -0
- package/dist/skills/hor-multi-llm-provider/references/provider-internals.md +153 -0
- package/dist/skills/hor-mutation-resolver/SKILL.md +360 -0
- package/dist/skills/hor-mutation-resolver/references/dependency-injection.md +148 -0
- package/dist/skills/hor-mutation-resolver/references/errors.md +120 -0
- package/dist/skills/hor-mutation-resolver/references/resolve-flow.md +275 -0
- package/dist/skills/hor-mutation-resolver/references/testing.md +125 -0
- package/dist/skills/hor-mutation-resolver/references/transaction.md +197 -0
- package/dist/skills/hor-post-worker/SKILL.md +320 -0
- package/dist/skills/hor-query-resolver/SKILL.md +246 -0
- package/dist/skills/hor-query-resolver/references/anatomy.md +384 -0
- package/dist/skills/hor-query-resolver/references/data-access.md +248 -0
- package/dist/skills/hor-query-resolver/references/errors-and-context.md +172 -0
- package/dist/skills/hor-query-resolver/references/testing.md +241 -0
- package/dist/skills/hor-renchan-job-bullmq/SKILL.md +178 -0
- package/dist/skills/hor-renchan-job-bullmq/references/daemon-and-scheduler.md +182 -0
- package/dist/skills/hor-renchan-job-bullmq/references/engine-and-infra.md +106 -0
- package/dist/skills/hor-renchan-job-bullmq/references/job-triple.md +189 -0
- package/dist/skills/hor-renchan-job-bullmq/references/queues.md +74 -0
- package/dist/skills/hor-renchan-job-bullmq/references/subscriptions.md +113 -0
- package/dist/skills/hor-resolver-share/SKILL.md +262 -0
- package/dist/skills/hor-resolver-validator/SKILL.md +194 -0
- package/dist/skills/hor-resolver-validator/references/inspector-api.md +115 -0
- package/dist/skills/hor-resolver-validator/references/validator-pattern.md +281 -0
- package/dist/skills/hor-restfulapi-architecture/SKILL.md +275 -0
- package/dist/skills/hor-restfulapi-architecture/references/engine-and-context.md +192 -0
- package/dist/skills/hor-restfulapi-architecture/references/renderers.md +204 -0
- package/dist/skills/hor-restfulapi-architecture/references/response-and-flusher.md +118 -0
- package/dist/skills/hor-restfulapi-architecture/references/validation.md +229 -0
- package/dist/skills/hor-security-audit/SKILL.md +110 -0
- package/dist/skills/hor-security-audit/references/application-surface.md +90 -0
- package/dist/skills/hor-security-audit/references/auth-and-transport.md +147 -0
- package/dist/skills/hor-security-audit/references/code-safety.md +74 -0
- package/dist/skills/hor-security-audit/references/dependencies.md +72 -0
- package/dist/skills/hor-security-audit/references/network-and-logging.md +56 -0
- package/dist/skills/hor-security-audit/references/secrets.md +95 -0
- package/dist/skills/hor-sequelize-migration/SKILL.md +336 -0
- package/dist/skills/hor-sequelize-migration/references/alter-table.md +147 -0
- package/dist/skills/hor-sequelize-migration/references/foreign-keys.md +107 -0
- package/dist/skills/hor-sequelize-migration/references/indexes.md +149 -0
- package/dist/skills/hor-sequelize-migration/references/notation.md +215 -0
- package/dist/skills/hor-sequelize-model/SKILL.md +69 -0
- package/dist/skills/hor-sequelize-model/references/associations.md +218 -0
- package/dist/skills/hor-sequelize-model/references/default-methods.md +244 -0
- package/dist/skills/hor-sequelize-model/references/mixins.md +247 -0
- package/dist/skills/hor-sequelize-model/references/notation.md +225 -0
- package/dist/skills/hor-sequelize-model/references/timestamps.md +74 -0
- package/dist/skills/hor-sequelize-seeder/SKILL.md +275 -0
- package/dist/skills/hor-sequelize-seeder/references/development-coverage.md +74 -0
- package/dist/skills/hor-sequelize-seeder/references/directory-structure.md +113 -0
- package/dist/skills/hor-sequelize-seeder/references/id-numbering.md +133 -0
- package/dist/skills/hor-sequelize-seeder/references/naming.md +59 -0
- package/dist/skills/hor-sequelize-seeder/references/notation.md +170 -0
- package/dist/skills/hor-sequelize-subquery/SKILL.md +369 -0
- package/dist/skills/hor-strategy-pattern/SKILL.md +371 -0
- package/dist/skills/hor-stub-api/SKILL.md +330 -0
- package/dist/skills/hor-subscription-resolver/SKILL.md +105 -0
- package/dist/skills/hor-subscription-resolver/references/authentication.md +99 -0
- package/dist/skills/hor-subscription-resolver/references/basic-notation.md +116 -0
- package/dist/skills/hor-subscription-resolver/references/design-philosophy.md +68 -0
- package/dist/skills/hor-type-interface/SKILL.md +200 -0
- package/docs/skills.ja.md +40 -0
- package/docs/skills.md +40 -0
- package/lib/equip/CommandLineArguments.js +178 -0
- package/lib/equip/HoraSkillsCli.js +590 -0
- package/lib/equip/SkillsInstaller.js +443 -0
- package/lib/equip/SkillsManifestFile.js +260 -0
- package/lib/equip/constants/CLI_COMMAND.js +8 -0
- package/lib/equip/hora-skills-ort-renchan.js +8 -0
- package/package.json +51 -0
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
# Testing (agent-loop testing guidelines)
|
|
2
|
+
|
|
3
|
+
The testing approach for actions / loops / composition / Worker / Dispatcher / Resolver written with
|
|
4
|
+
`@openreachtech/mentsu-agent-loop-*`. Referenced from `SKILL.md`. The specs of the target classes are in
|
|
5
|
+
[core.md](./core.md) / [renchan-job-integration.md](./renchan-job-integration.md) /
|
|
6
|
+
[graphql-integration.md](./graphql-integration.md).
|
|
7
|
+
|
|
8
|
+
These follow the **`hoc-jest` skill** — read it for the general rules; this file only
|
|
9
|
+
applies them to the agent-loop classes. In short: one file per class; nest
|
|
10
|
+
`describe(ClassName) > describe('#member()' / '.get:member') > describe('should …') > test.each(cases)`;
|
|
11
|
+
write each test as **Arrange / Act / Assert** separated by blank lines, taking the Act result into a
|
|
12
|
+
`received` variable; bind every `jest.fn()` to a `~Spy` variable; and keep `cases` data unique and
|
|
13
|
+
index-readable (`fake-keyword-0001`, `100001`, …).
|
|
14
|
+
|
|
15
|
+
## Grand principle: swap `context` for a stub and touch no real I/O
|
|
16
|
+
|
|
17
|
+
Because this library is deps:0 with `context` DI, **everything can be unit-tested without touching AI,
|
|
18
|
+
DB, Redis, or the network**. Tests always stub `context` and never hit external systems.
|
|
19
|
+
|
|
20
|
+
- **Why**: since the iteration logic reaches dependencies through `context` rather than importing them
|
|
21
|
+
directly (the grand principle in `SKILL.md`), a test just fakes `context` and becomes fast and
|
|
22
|
+
deterministic. Tests that touch real I/O are slow, flaky, and throw away this design's benefit.
|
|
23
|
+
- Test a Worker by **`new`-ing it directly** rather than going through the BullMQ factory (below). Do
|
|
24
|
+
not start a daemon or Redis.
|
|
25
|
+
|
|
26
|
+
## The contract test kit (secure the baseline first)
|
|
27
|
+
|
|
28
|
+
The core `@openreachtech/mentsu-agent-loop-core/lib/testing/index.js` provides `LoopContractAssertion` /
|
|
29
|
+
`ActionContractAssertion`. Calling `create({ createInstance }).assert()` inside a jest test verifies the
|
|
30
|
+
base contract (inheritance, required getters, presence of abstract hooks, validity of QC values).
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import {
|
|
34
|
+
LoopContractAssertion,
|
|
35
|
+
} from '@openreachtech/mentsu-agent-loop-core/lib/testing/index.js'
|
|
36
|
+
|
|
37
|
+
import ResearchAgentLoop from '../../app/agentLoops/ResearchAgentLoop.js'
|
|
38
|
+
|
|
39
|
+
describe('ResearchAgentLoop', () => {
|
|
40
|
+
describe('loop contract', () => {
|
|
41
|
+
test('should satisfy the base loop contract', () => {
|
|
42
|
+
const assertion = LoopContractAssertion.create({
|
|
43
|
+
createInstance: () => ResearchAgentLoop.create({
|
|
44
|
+
context: {
|
|
45
|
+
aiAgent: {},
|
|
46
|
+
searchClient: {},
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const received = assertion.assert()
|
|
52
|
+
|
|
53
|
+
expect(received)
|
|
54
|
+
.toBeTruthy()
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
- What `LoopContractAssertion` checks: `instanceof BaseAgentLoop` / `maxIterations` is an integer ≥ 1 /
|
|
61
|
+
`compressionInterval` is `null` or an integer ≥ 1 / `run`, `createInitialState`, `advanceState`,
|
|
62
|
+
`isComplete`, `buildResult` are functions.
|
|
63
|
+
- What `ActionContractAssertion` checks: `instanceof BaseAgentAction` / `name` and `description` are
|
|
64
|
+
non-empty strings / `run` is a function.
|
|
65
|
+
- **Why put the contract test first**: before testing individual logic, it catches base-contract
|
|
66
|
+
violations (an unimplemented hook, an invalid `maxIterations`) in one line. The contract test is the
|
|
67
|
+
**baseline**, not coverage. Test the logic separately.
|
|
68
|
+
|
|
69
|
+
## Unit-test approach per class
|
|
70
|
+
|
|
71
|
+
### Action (`BaseAgentAction`)
|
|
72
|
+
|
|
73
|
+
`Action.create()` and call `run({ argumentHash, context, onProgress })`, asserting that the **context
|
|
74
|
+
stub is called as expected and the return value is correct**, and that **`onProgress` fires**.
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
describe('SearchAction', () => {
|
|
78
|
+
describe('#run()', () => {
|
|
79
|
+
describe('should search via the injected client', () => {
|
|
80
|
+
const cases = [
|
|
81
|
+
{
|
|
82
|
+
tally: {
|
|
83
|
+
keyword: 'fake-keyword-0001',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
tally: {
|
|
88
|
+
keyword: 'fake-keyword-0002',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
test.each(cases)('keyword: $tally.keyword', async ({ tally }) => {
|
|
94
|
+
const searchSpy = jest.fn()
|
|
95
|
+
.mockResolvedValue({
|
|
96
|
+
items: [], // neutral; not under test
|
|
97
|
+
})
|
|
98
|
+
const args = {
|
|
99
|
+
argumentHash: tally,
|
|
100
|
+
context: {
|
|
101
|
+
searchClient: {
|
|
102
|
+
search: searchSpy,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
onProgress: jest.fn(),
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
await SearchAction.create()
|
|
109
|
+
.run(args)
|
|
110
|
+
|
|
111
|
+
expect(searchSpy)
|
|
112
|
+
.toHaveBeenCalledWith(tally)
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
describe('should resolve the search result', () => {
|
|
117
|
+
const cases = [
|
|
118
|
+
{
|
|
119
|
+
input: {
|
|
120
|
+
keyword: 'fake-keyword-0001',
|
|
121
|
+
},
|
|
122
|
+
expected: {
|
|
123
|
+
items: [
|
|
124
|
+
'fake-item-0001',
|
|
125
|
+
],
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
input: {
|
|
130
|
+
keyword: 'fake-keyword-0002',
|
|
131
|
+
},
|
|
132
|
+
expected: {
|
|
133
|
+
items: [
|
|
134
|
+
'fake-item-0002',
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
]
|
|
139
|
+
|
|
140
|
+
test.each(cases)('keyword: $input.keyword', async ({ input, expected }) => {
|
|
141
|
+
const searchSpy = jest.fn()
|
|
142
|
+
.mockResolvedValue(expected)
|
|
143
|
+
const args = {
|
|
144
|
+
argumentHash: {
|
|
145
|
+
keyword: input.keyword,
|
|
146
|
+
},
|
|
147
|
+
context: {
|
|
148
|
+
searchClient: {
|
|
149
|
+
search: searchSpy,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
onProgress: jest.fn(),
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const received = await SearchAction.create()
|
|
156
|
+
.run(args)
|
|
157
|
+
|
|
158
|
+
expect(received)
|
|
159
|
+
.toEqual(expected)
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Verify `onProgress` fires with a third `describe('should emit progress')` of the same shape — bind
|
|
167
|
+
the callback to `onProgressSpy` and assert `.toHaveBeenCalled()`.
|
|
168
|
+
|
|
169
|
+
### Loop (`BaseAgentLoop` subclasses)
|
|
170
|
+
|
|
171
|
+
Look at it in two layers.
|
|
172
|
+
|
|
173
|
+
1. **The whole `run`**: with a fake `context`, call `run({ input, onProgress })` and assert the final
|
|
174
|
+
result, the **stop (`isComplete` / `maxIterations`)**, and **progress firing**. In a separate case,
|
|
175
|
+
confirm that even when the stop condition keeps returning false, it stops at `maxIterations` (the
|
|
176
|
+
infinite-loop safety bound).
|
|
177
|
+
2. **Individual hooks**: call `createInitialState` / `isComplete` / `buildResult` (and `decideNext` /
|
|
178
|
+
`reduceActionResult` for AI-driven, or `advanceState` for procedural) directly, and check the
|
|
179
|
+
immutable state update and the branching.
|
|
180
|
+
|
|
181
|
+
```js
|
|
182
|
+
// maxIterations safety bound: stops even when isComplete is always false.
|
|
183
|
+
// Override via jest.spyOn (not a test-only subclass); test.each proves the bound
|
|
184
|
+
// reads maxIterations rather than a hardcoded number.
|
|
185
|
+
describe('ResearchAgentLoop', () => {
|
|
186
|
+
describe('#run()', () => {
|
|
187
|
+
describe('should stop at maxIterations when never complete', () => {
|
|
188
|
+
const cases = [
|
|
189
|
+
{
|
|
190
|
+
override: {
|
|
191
|
+
maxIterations: 2,
|
|
192
|
+
},
|
|
193
|
+
expected: 2,
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
override: {
|
|
197
|
+
maxIterations: 3,
|
|
198
|
+
},
|
|
199
|
+
expected: 3,
|
|
200
|
+
},
|
|
201
|
+
]
|
|
202
|
+
|
|
203
|
+
test.each(cases)('maxIterations: $override.maxIterations', async ({ override, expected }) => {
|
|
204
|
+
jest.spyOn(ResearchAgentLoop.prototype, 'maxIterations', 'get')
|
|
205
|
+
.mockReturnValue(override.maxIterations)
|
|
206
|
+
jest.spyOn(ResearchAgentLoop.prototype, 'isComplete')
|
|
207
|
+
.mockReturnValue(false) // never complete → forces the safety bound
|
|
208
|
+
|
|
209
|
+
const loop = ResearchAgentLoop.create({
|
|
210
|
+
context: {
|
|
211
|
+
aiAgent: {},
|
|
212
|
+
searchClient: {},
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
const args = {
|
|
216
|
+
input: {
|
|
217
|
+
keyword: 'fake-keyword-0001',
|
|
218
|
+
},
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const result = await loop.run(args)
|
|
222
|
+
const received = result.iterations
|
|
223
|
+
|
|
224
|
+
expect(received)
|
|
225
|
+
.toBe(expected)
|
|
226
|
+
})
|
|
227
|
+
})
|
|
228
|
+
})
|
|
229
|
+
})
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Composition (`BaseAgentPipeline` / `BaseCompositeAgent` / `BaseActionStage`)
|
|
233
|
+
|
|
234
|
+
- **Pipeline**: that `get stageCtors` returns the expected stage list, and that `run` threads the payload
|
|
235
|
+
across stages into the final `buildResult`. Stage Ctors may be stubs (fake Runnables with `create` /
|
|
236
|
+
`run`).
|
|
237
|
+
- **Composite**: the split from `planSubTasks`, the combine in `mergeResults`, and the `concurrency`
|
|
238
|
+
value.
|
|
239
|
+
- **ActionStage**: `static get ActionCtor` / `buildArgumentHash` (payload → arguments) / `buildOutput`
|
|
240
|
+
(result → output).
|
|
241
|
+
|
|
242
|
+
### Worker (`BaseAgentJobWorker` subclasses)
|
|
243
|
+
|
|
244
|
+
**Do not** go through the BullMQ factory; fake the engine / broker and **`new` it directly** in each
|
|
245
|
+
test's Arrange. Do **not** extract a `createWorker()` helper into the test file — a helper defined in
|
|
246
|
+
the test file is unverified logic (see the `hoc-jest` skill's anti-pattern); if the construction is
|
|
247
|
+
genuinely reused, put the builder under `tests/tools/` with its own test.
|
|
248
|
+
|
|
249
|
+
Behaviors to verify:
|
|
250
|
+
|
|
251
|
+
- **`buildOptionHash()`**: that `concurrency` / `limiter` from `config` are forwarded to the BullMQ
|
|
252
|
+
options (needed because the base only passes `connection`).
|
|
253
|
+
- **`buildTopic({ scope })`**: `channel` when `scope` is `null`, otherwise `channel:key=value` (keys
|
|
254
|
+
sorted ascending).
|
|
255
|
+
- **`onJobProgress` / `onJobCompleted` / `onJobFailed`**: that the broker's `publish` is called with the
|
|
256
|
+
**correct channel and event shape** (completed is `{ phase: 'done', payload: result }`, failed is
|
|
257
|
+
`{ phase: 'error', payload: { message } }`).
|
|
258
|
+
- **`executeJob({ body, context, parcel })`**: that the loop runs and returns a result, and progress
|
|
259
|
+
flows to `parcel.jobModel.job.updateProgress`.
|
|
260
|
+
- **If you overrode `publishProgress`** (e.g. to match renchan's broker shape), verify that call shape
|
|
261
|
+
too.
|
|
262
|
+
|
|
263
|
+
```js
|
|
264
|
+
describe('ResearchJobWorker', () => {
|
|
265
|
+
describe('#executeJob()', () => {
|
|
266
|
+
describe('should stream progress to the job', () => {
|
|
267
|
+
const cases = [
|
|
268
|
+
{
|
|
269
|
+
input: {
|
|
270
|
+
jobId: 100001,
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
input: {
|
|
275
|
+
jobId: 100002,
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
]
|
|
279
|
+
|
|
280
|
+
test.each(cases)('jobId: $input.jobId', async ({ input }) => {
|
|
281
|
+
const worker = new ResearchJobWorker({
|
|
282
|
+
engine: {
|
|
283
|
+
subscriptionBroker: {
|
|
284
|
+
publish: jest.fn(),
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
config: {
|
|
288
|
+
connection: {
|
|
289
|
+
host: 'localhost',
|
|
290
|
+
port: 6379,
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
manifest: null,
|
|
294
|
+
dispatcherHash: {},
|
|
295
|
+
errorHash: {},
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
const updateProgressSpy = jest.fn()
|
|
299
|
+
const args = {
|
|
300
|
+
body: {
|
|
301
|
+
jobId: input.jobId,
|
|
302
|
+
},
|
|
303
|
+
context: {},
|
|
304
|
+
parcel: {
|
|
305
|
+
jobModel: {
|
|
306
|
+
job: {
|
|
307
|
+
updateProgress: updateProgressSpy,
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
await worker.executeJob(args)
|
|
314
|
+
|
|
315
|
+
expect(updateProgressSpy)
|
|
316
|
+
.toHaveBeenCalled()
|
|
317
|
+
})
|
|
318
|
+
})
|
|
319
|
+
})
|
|
320
|
+
})
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Verify the return value with a sibling `describe('should return a result')` — take
|
|
324
|
+
`const received = await worker.executeJob(args)` and assert `.toBeDefined()`.
|
|
325
|
+
|
|
326
|
+
### Dispatcher (`BaseAgentJobDispatcher` subclasses)
|
|
327
|
+
|
|
328
|
+
- That `static get EngineCtor` / `static get ManifestCtor` return the correct classes.
|
|
329
|
+
- That `dispatchAgentJob({ input })` delegates to `dispatchJob({ body: input, optionHash })` (stub
|
|
330
|
+
`dispatchJob` and check the arguments; do not actually enqueue).
|
|
331
|
+
|
|
332
|
+
### Mutation resolver (`BaseRequestAgentMutationResolver` subclasses)
|
|
333
|
+
|
|
334
|
+
Pass a fake `context` whose `share` carries a stubbed dispatch provider, and verify that
|
|
335
|
+
`resolve({ variables, context })` **dispatches with the body from `buildJobBody`** and returns
|
|
336
|
+
**`{ accepted, jobId }`**. (This matches sourcing the dispatcher from `context.share`; see the note in
|
|
337
|
+
[graphql-integration.md](./graphql-integration.md).)
|
|
338
|
+
|
|
339
|
+
```js
|
|
340
|
+
describe('RequestResearchMutationResolver', () => {
|
|
341
|
+
describe('#resolve()', () => {
|
|
342
|
+
describe('should return acceptance with the dispatched job id', () => {
|
|
343
|
+
const cases = [
|
|
344
|
+
{
|
|
345
|
+
input: {
|
|
346
|
+
jobId: 100001,
|
|
347
|
+
},
|
|
348
|
+
expected: {
|
|
349
|
+
accepted: true,
|
|
350
|
+
jobId: 'fake-job-id-0001',
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
input: {
|
|
355
|
+
jobId: 100002,
|
|
356
|
+
},
|
|
357
|
+
expected: {
|
|
358
|
+
accepted: true,
|
|
359
|
+
jobId: 'fake-job-id-0002',
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
]
|
|
363
|
+
|
|
364
|
+
test.each(cases)('jobId: $input.jobId', async ({ input, expected }) => {
|
|
365
|
+
const resolver = new RequestResearchMutationResolver({
|
|
366
|
+
errorHash: {},
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
const dispatchJobSpy = jest.fn()
|
|
370
|
+
.mockResolvedValue({
|
|
371
|
+
hasResponse: () => true,
|
|
372
|
+
idKey: expected.jobId,
|
|
373
|
+
})
|
|
374
|
+
const args = {
|
|
375
|
+
variables: {
|
|
376
|
+
input: {
|
|
377
|
+
jobId: input.jobId,
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
context: {
|
|
381
|
+
share: {
|
|
382
|
+
jobDispatcherProvider: {
|
|
383
|
+
dispatchJob: dispatchJobSpy,
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const received = await resolver.resolve(args)
|
|
390
|
+
|
|
391
|
+
expect(received)
|
|
392
|
+
.toEqual(expected)
|
|
393
|
+
})
|
|
394
|
+
})
|
|
395
|
+
})
|
|
396
|
+
})
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
Verify the dispatch itself with a sibling `describe('should dispatch the job body')` — assert
|
|
400
|
+
`dispatchJobSpy` was called with `{ DispatcherCtor: ResearchJobDispatcher, body: <buildJobBody> }`.
|
|
401
|
+
|
|
402
|
+
### Subscription resolver (`BaseAgentProgressSubscriptionResolver` subclasses) — always verify channel match
|
|
403
|
+
|
|
404
|
+
Verify that `generateChannel` / `buildTopic` produce **the same string as the `AgentTopic` the Worker
|
|
405
|
+
builds from**, by comparing against a hand-built `AgentTopic` expected value. This is the only guard
|
|
406
|
+
against publish/subscribe drift.
|
|
407
|
+
|
|
408
|
+
```js
|
|
409
|
+
import {
|
|
410
|
+
AgentTopic,
|
|
411
|
+
} from '@openreachtech/mentsu-agent-loop-core'
|
|
412
|
+
|
|
413
|
+
describe('OnResearchProgressSubscriptionResolver', () => {
|
|
414
|
+
describe('#generateChannel()', () => {
|
|
415
|
+
describe('should match the channel the worker publishes to', () => {
|
|
416
|
+
const cases = [
|
|
417
|
+
{
|
|
418
|
+
input: {
|
|
419
|
+
query: {
|
|
420
|
+
userId: 100001,
|
|
421
|
+
},
|
|
422
|
+
},
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
input: {
|
|
426
|
+
query: {
|
|
427
|
+
userId: 100002,
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
},
|
|
431
|
+
]
|
|
432
|
+
|
|
433
|
+
test.each(cases)('userId: $input.query.userId', ({ input }) => {
|
|
434
|
+
const resolver = new OnResearchProgressSubscriptionResolver({
|
|
435
|
+
errorHash: {},
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
const expected = AgentTopic.create({
|
|
439
|
+
channel: 'researchProgress', // the same channel the Worker builds from
|
|
440
|
+
scope: input.query,
|
|
441
|
+
})
|
|
442
|
+
.value
|
|
443
|
+
const args = {
|
|
444
|
+
query: input.query,
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const received = resolver.generateChannel(args)
|
|
448
|
+
|
|
449
|
+
expect(received)
|
|
450
|
+
.toBe(expected)
|
|
451
|
+
})
|
|
452
|
+
})
|
|
453
|
+
})
|
|
454
|
+
})
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
- Also confirm that `generateChannelQuery({ variables })` returns the expected scope, and that
|
|
458
|
+
`get channel()` is the same string as the Worker's.
|
|
459
|
+
- **Why compare against a hand-built `AgentTopic`**: even though the Worker and the Subscription are
|
|
460
|
+
implemented independently, one test pins that both resolve to the same channel string. The moment
|
|
461
|
+
`channel` or a scope key is changed on only one side, this test fails.
|
|
462
|
+
|
|
463
|
+
### Runner
|
|
464
|
+
|
|
465
|
+
- `InlineAgentRunner`: that `request({ input, onProgress })` runs the loop and returns the final result.
|
|
466
|
+
- `JobAgentRunner`: stub `DispatcherCtor.createAsync` / `dispatchJob` / `teardown`, and verify that
|
|
467
|
+
`request({ input })` enqueues, returns `{ accepted, jobId }`, and calls `teardown`.
|
|
468
|
+
|
|
469
|
+
## Verifying unimplemented abstracts
|
|
470
|
+
|
|
471
|
+
Touching an abstract member of a base class throws `ConcreteMemberNotFoundError`. Assert this with
|
|
472
|
+
`expect(operation).toThrow(ConcreteMemberNotFoundError)` to guarantee **the subclass overrides it
|
|
473
|
+
correctly**.
|
|
474
|
+
|
|
475
|
+
```js
|
|
476
|
+
describe('BaseAgentJobWorker', () => {
|
|
477
|
+
describe('.get:AgentLoopCtor', () => {
|
|
478
|
+
describe('when not inherited', () => {
|
|
479
|
+
test('should throw error', () => {
|
|
480
|
+
const operation = () => BaseAgentJobWorker.AgentLoopCtor
|
|
481
|
+
|
|
482
|
+
expect(operation)
|
|
483
|
+
.toThrow(ConcreteMemberNotFoundError)
|
|
484
|
+
})
|
|
485
|
+
})
|
|
486
|
+
})
|
|
487
|
+
})
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
- **Why**: with both the contract test (the kit) and individual tests, "forgot to implement" is detected
|
|
491
|
+
at runtime rather than by types. This library has no true `final` and works around gaps via override,
|
|
492
|
+
so the unimplemented is detected by a throw.
|