@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,291 @@
|
|
|
1
|
+
# The SDK-wrapping pattern (an interim implementation)
|
|
2
|
+
|
|
3
|
+
What to do when the vendor publishes an official SDK (`@google/genai`, the AWS SDK, …). Rather than
|
|
4
|
+
hitting HTTP directly, **the launcher holds the SDK** and calls it. The way differences are absorbed
|
|
5
|
+
(input in the `Payload`, output in the `Capsule`) is the same as in the direct-HTTP pattern; **only the
|
|
6
|
+
transport step changes, from `fetch` to an SDK call**. Referenced from [SKILL.md](../SKILL.md). Direct
|
|
7
|
+
HTTP is in [launcher.md](./launcher.md).
|
|
8
|
+
|
|
9
|
+
## ⚠️ This is interim. Migrate once it is supported properly
|
|
10
|
+
|
|
11
|
+
rocket-client does **not support** the SDK-wrapping shape at present. This section is a **temporary
|
|
12
|
+
workaround that overrides `BaseLauncher.launchRequest()`** — the `fetch` flow — and substitutes an SDK
|
|
13
|
+
call.
|
|
14
|
+
|
|
15
|
+
- Migrate as soon as an update supports it properly.
|
|
16
|
+
- **Keep it minimal, because it is interim** — add extensions such as holding the error only when you
|
|
17
|
+
actually need them.
|
|
18
|
+
- **Limit where it is used**: only where an official SDK is provided. If REST can be hit directly, use
|
|
19
|
+
the direct-HTTP pattern and do not override `launchRequest()`.
|
|
20
|
+
- The existing `app/geminiClient/` is an SDK wrapper of the same kind.
|
|
21
|
+
|
|
22
|
+
## Launcher: hold the SDK and override `launchRequest()`
|
|
23
|
+
|
|
24
|
+
- Extend `constructor` / `create()` to hold the SDK instance. Produce the default from a factory
|
|
25
|
+
(`createSdkClient()`) so that **a test can swap it with `create({ sdkClient })`**.
|
|
26
|
+
- Override `launchRequest()` so that instead of the `fetch` flow (`createFetchRequest` → `fetch` → JSON
|
|
27
|
+
parsing), it **calls the SDK method and builds the `Capsule` straight from the result**.
|
|
28
|
+
- The raw object the SDK returns (the one with getters like `.text`) is **stored in `rawResponse` without
|
|
29
|
+
going through schema normalization**. The `Capsule` reads it from `this.rawResponse` directly.
|
|
30
|
+
- Keep calling the input validation (`hasInvalidParameterHash()`) after the override, and on invalid
|
|
31
|
+
input return an error `Capsule` without calling the SDK. When the SDK call throws, return a `Capsule`
|
|
32
|
+
with `rawResponse: null` so that `hasError()` is `true`.
|
|
33
|
+
- **Why make it injectable**: the SDK really does perform the external communication, so a test cannot
|
|
34
|
+
call the real one. Only with `create({ sdkClient })` can the launcher's behaviour be verified against a
|
|
35
|
+
stub SDK ([testing.md](./testing.md)).
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import {
|
|
39
|
+
GoogleGenAI,
|
|
40
|
+
} from '@google/genai'
|
|
41
|
+
|
|
42
|
+
import {
|
|
43
|
+
env,
|
|
44
|
+
} from '../globals/_.js'
|
|
45
|
+
|
|
46
|
+
import {
|
|
47
|
+
BaseLauncher,
|
|
48
|
+
} from '@openreachtech/mentsu-rocket-client'
|
|
49
|
+
|
|
50
|
+
import SendMessageToGeminiPayload from './SendMessageToGeminiPayload.js'
|
|
51
|
+
import SendMessageToGeminiCapsule from './SendMessageToGeminiCapsule.js'
|
|
52
|
+
|
|
53
|
+
export default class SendMessageToGeminiLauncher extends BaseLauncher {
|
|
54
|
+
/**
|
|
55
|
+
* Constructor.
|
|
56
|
+
*
|
|
57
|
+
* @param {{
|
|
58
|
+
* config: Record<string, *>
|
|
59
|
+
* sdkClient: import('@google/genai').GoogleGenAI
|
|
60
|
+
* }} params
|
|
61
|
+
*/
|
|
62
|
+
constructor ({
|
|
63
|
+
config,
|
|
64
|
+
sdkClient,
|
|
65
|
+
}) {
|
|
66
|
+
super({
|
|
67
|
+
config,
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
this.sdkClient = sdkClient
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Factory method.
|
|
75
|
+
*
|
|
76
|
+
* @override
|
|
77
|
+
* @param {{
|
|
78
|
+
* config?: Record<string, *>
|
|
79
|
+
* sdkClient?: import('@google/genai').GoogleGenAI
|
|
80
|
+
* }} [params]
|
|
81
|
+
* @returns {SendMessageToGeminiLauncher}
|
|
82
|
+
*/
|
|
83
|
+
static create ({
|
|
84
|
+
config = this.clientConfig,
|
|
85
|
+
sdkClient = this.createSdkClient({
|
|
86
|
+
config,
|
|
87
|
+
}),
|
|
88
|
+
} = {}) {
|
|
89
|
+
return new this({
|
|
90
|
+
config,
|
|
91
|
+
sdkClient,
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* get: client configuration.
|
|
97
|
+
*
|
|
98
|
+
* @override
|
|
99
|
+
* @returns {{
|
|
100
|
+
* LLM_API_KEY: string
|
|
101
|
+
* }}
|
|
102
|
+
*/
|
|
103
|
+
static get clientConfig () {
|
|
104
|
+
return {
|
|
105
|
+
LLM_API_KEY: env.LLM_API_KEY,
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @override */
|
|
110
|
+
static get Payload () {
|
|
111
|
+
return SendMessageToGeminiPayload
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @override */
|
|
115
|
+
static get Capsule () {
|
|
116
|
+
return SendMessageToGeminiCapsule
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Create the vendor SDK client (transport).
|
|
121
|
+
*
|
|
122
|
+
* @param {{
|
|
123
|
+
* config: {
|
|
124
|
+
* LLM_API_KEY: string
|
|
125
|
+
* }
|
|
126
|
+
* }} params
|
|
127
|
+
* @returns {import('@google/genai').GoogleGenAI}
|
|
128
|
+
*/
|
|
129
|
+
static createSdkClient ({
|
|
130
|
+
config,
|
|
131
|
+
}) {
|
|
132
|
+
return new GoogleGenAI({
|
|
133
|
+
apiKey: config.LLM_API_KEY,
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Launch request via the SDK (provisional override of the fetch flow).
|
|
139
|
+
*
|
|
140
|
+
* @override
|
|
141
|
+
* @param {{
|
|
142
|
+
* payload: SendMessageToGeminiPayload
|
|
143
|
+
* }} params
|
|
144
|
+
* @returns {Promise<SendMessageToGeminiCapsule>}
|
|
145
|
+
*/
|
|
146
|
+
async launchRequest ({
|
|
147
|
+
payload,
|
|
148
|
+
}) {
|
|
149
|
+
if (payload.hasInvalidParameterHash()) {
|
|
150
|
+
return this.Ctor.createCapsuleAsInvalidInputError({
|
|
151
|
+
payload,
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
const result = await this.sdkClient
|
|
157
|
+
.models
|
|
158
|
+
.generateContent(
|
|
159
|
+
payload.toSdkParams()
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
return this.Ctor.createCapsule({
|
|
163
|
+
payload,
|
|
164
|
+
rawResponse: result,
|
|
165
|
+
rawBody: null,
|
|
166
|
+
})
|
|
167
|
+
} catch (error) {
|
|
168
|
+
return this.Ctor.createCapsule({
|
|
169
|
+
payload,
|
|
170
|
+
rawResponse: null,
|
|
171
|
+
rawBody: null,
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Payload: still extends `BasePayload`, as a container with `toSdkParams()`
|
|
179
|
+
|
|
180
|
+
Even for an SDK call the `Payload` extends `BasePayload`, because `Capsule`'s `hasError()` and friends
|
|
181
|
+
consult the payload's `hasAuthorization()` / `hasInvalidParameterHash()`. The parameters for the SDK call
|
|
182
|
+
are returned from `toSdkParams()`.
|
|
183
|
+
|
|
184
|
+
- `method` / `pathname` / `contentType` are unused by an SDK call, but `BasePayload.create()` consults
|
|
185
|
+
them, so they are still declared (interim).
|
|
186
|
+
- **Why extend `BasePayload`**: to keep the same split of responsibilities in an SDK wrapper — input
|
|
187
|
+
validation in the payload, extraction in the capsule. Drop the inheritance for a plain object and the
|
|
188
|
+
payload interface that `Capsule.hasError()` assumes is missing, and it breaks.
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
import {
|
|
192
|
+
KeywordScalar,
|
|
193
|
+
} from '@openreachtech/mentsu-schema'
|
|
194
|
+
|
|
195
|
+
import {
|
|
196
|
+
BasePayload,
|
|
197
|
+
REST_METHOD,
|
|
198
|
+
} from '@openreachtech/mentsu-rocket-client'
|
|
199
|
+
|
|
200
|
+
export default class SendMessageToGeminiPayload extends BasePayload {
|
|
201
|
+
/** @type {Record<string, *>} */
|
|
202
|
+
static bodySchema = {
|
|
203
|
+
model: KeywordScalar,
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Interim: unused by the SDK call, but declared because BasePayload.create() consults it.
|
|
207
|
+
/** @override */
|
|
208
|
+
static get method () {
|
|
209
|
+
return REST_METHOD.POST
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** @override */
|
|
213
|
+
static get pathname () {
|
|
214
|
+
return '/'
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** @override */
|
|
218
|
+
static get contentType () {
|
|
219
|
+
return 'application/json'
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* SDK params.
|
|
224
|
+
*
|
|
225
|
+
* @returns {Record<string, *>}
|
|
226
|
+
*/
|
|
227
|
+
toSdkParams () {
|
|
228
|
+
return this.requestBody.body
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
The caller passes the SDK parameters as `body`:
|
|
234
|
+
|
|
235
|
+
```js
|
|
236
|
+
const payload = SendMessageToGeminiPayload.create({
|
|
237
|
+
body: {
|
|
238
|
+
model: 'gemini-2.5-flash',
|
|
239
|
+
contents: 'fake-contents-001',
|
|
240
|
+
},
|
|
241
|
+
})
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Capsule: extract from `this.rawResponse` (the SDK object)
|
|
245
|
+
|
|
246
|
+
In an SDK wrapper, `extractXxx()` reads not the JSON-normalized `this.body` but `this.rawResponse` — the
|
|
247
|
+
raw object the SDK returns, with getters like `.text`. The conventions for null guards and defaults are
|
|
248
|
+
the same as for direct HTTP ([capsule.md](./capsule.md)).
|
|
249
|
+
|
|
250
|
+
```js
|
|
251
|
+
import {
|
|
252
|
+
BaseCapsule,
|
|
253
|
+
} from '@openreachtech/mentsu-rocket-client'
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* @extends {BaseCapsule<*, *>}
|
|
257
|
+
*/
|
|
258
|
+
export default class SendMessageToGeminiCapsule extends BaseCapsule {
|
|
259
|
+
/**
|
|
260
|
+
* Extract content text from the SDK response.
|
|
261
|
+
*
|
|
262
|
+
* @returns {string}
|
|
263
|
+
*/
|
|
264
|
+
extractContentText () {
|
|
265
|
+
return this.rawResponse?.text
|
|
266
|
+
?? ''
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Extract function calls from the SDK response.
|
|
271
|
+
*
|
|
272
|
+
* @returns {Array<{
|
|
273
|
+
* name: string
|
|
274
|
+
* arguments: object
|
|
275
|
+
* }>}
|
|
276
|
+
*/
|
|
277
|
+
extractFunctionCalls () {
|
|
278
|
+
return this.rawResponse?.functionCalls
|
|
279
|
+
?.map(functionCall => ({
|
|
280
|
+
name: functionCall.name,
|
|
281
|
+
arguments: functionCall.args,
|
|
282
|
+
}))
|
|
283
|
+
?? []
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
- When the SDK call throws, `launchRequest()` returns a `Capsule` with `rawResponse: null` — the
|
|
289
|
+
equivalent of a network error — so `capsule.hasError()` is `true`. Pulling out the SDK's error message
|
|
290
|
+
and the like needs an extension, such as adding a factory to the `Capsule` that holds the error; keep
|
|
291
|
+
it minimal while this is interim.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Testing (tests for Payload / Capsule / Launcher)
|
|
2
|
+
|
|
3
|
+
The unit-test conventions for an external API client. The files live under
|
|
4
|
+
`tests/__tests__/app/<serviceName>Client/`, one per class. The general Jest conventions come from the
|
|
5
|
+
`hoc-jest` skill; this file states only what is specific to this module. Referenced from
|
|
6
|
+
[SKILL.md](../SKILL.md).
|
|
7
|
+
|
|
8
|
+
- Use `test.each()`, and make the test data unique with explicit fake values (`fake-...`).
|
|
9
|
+
- **Always stub the external communication**: replace `fetch` for direct HTTP, or `sdkClient` for an
|
|
10
|
+
SDK wrapper. Never write a test that hits the real thing (the mocking conventions of the `hoc-jest`
|
|
11
|
+
skill).
|
|
12
|
+
|
|
13
|
+
## What to test on a payload
|
|
14
|
+
|
|
15
|
+
- That it extends `BasePayload` (inheritance).
|
|
16
|
+
- That `method` / `pathname` / `contentType` are correct.
|
|
17
|
+
- That `querySchema` / `bodySchema` hold the expected keys.
|
|
18
|
+
- That the authentication members (`AuthorizationBuilderCtor` / `authorizationApiKey`) are correct.
|
|
19
|
+
- That after `create(...)`, `hasInvalidParameterHash()` is what it should be (`true` when a path
|
|
20
|
+
parameter is missing).
|
|
21
|
+
- **Why test the validation too**: the payload's responsibility is to absorb the input differences and
|
|
22
|
+
reject invalid input before sending. Without asserting the schema keys and
|
|
23
|
+
`hasInvalidParameterHash()`, there is no signal when that absorption breaks.
|
|
24
|
+
|
|
25
|
+
## What to test on a capsule
|
|
26
|
+
|
|
27
|
+
- That it extends `BaseCapsule`.
|
|
28
|
+
- For each `extractXxx()`, split "the value is present" and "the value is absent (returns the default /
|
|
29
|
+
`null`)" into separate `describe` blocks. Build one with
|
|
30
|
+
`Capsule.create({ rawResponse, payload, rawBody })`.
|
|
31
|
+
- That `hasError()` is `true` for a status code (>= 400), a network error and an input error.
|
|
32
|
+
- **Why the absent case is mandatory**: the null guard and the default in `extractXxx()`
|
|
33
|
+
([capsule.md](./capsule.md#extractxxx-always-guards-against-null-and-returns-null--an-empty-array-when-the-value-is-missing))
|
|
34
|
+
only matter when the response is missing something. Asserting only the present case lets a missing guard through —
|
|
35
|
+
`body` is `null` and it throws a `TypeError`.
|
|
36
|
+
|
|
37
|
+
## What to test on a launcher
|
|
38
|
+
|
|
39
|
+
- That the base launcher extends `BaseLauncher`, and that `clientConfig` returns the expected
|
|
40
|
+
connection settings.
|
|
41
|
+
- That the derived launcher's `Payload` / `Capsule` return the right classes.
|
|
42
|
+
- That `launchRequest()`, with **`fetch` stubbed** (by replacing `static get fetch ()`), is called with
|
|
43
|
+
the correct `Request` (URL, method, headers, body) and returns a `Capsule`.
|
|
44
|
+
- For an SDK wrapper ([sdk-wrapper.md](./sdk-wrapper.md)), inject a stub SDK through
|
|
45
|
+
`create({ sdkClient })` and assert that `launchRequest()` calls the SDK method with the right
|
|
46
|
+
arguments and returns the result carried in a `Capsule`.
|
|
47
|
+
|
|
48
|
+
## Replace `fetch` by overriding it in a sub-class
|
|
49
|
+
|
|
50
|
+
Stub `fetch` by creating an anonymous class inside the test that extends `CreateDocumentLauncher` and
|
|
51
|
+
overrides `static get fetch ()`. Have `jest.fn().mockResolvedValue(...)` return a `Response`, then assert
|
|
52
|
+
that it was called and what the `Capsule` extracted.
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
import CreateDocumentLauncher from '../../../../app/documentApiClient/CreateDocumentLauncher.js'
|
|
56
|
+
import CreateDocumentPayload from '../../../../app/documentApiClient/CreateDocumentPayload.js'
|
|
57
|
+
import CreateDocumentCapsule from '../../../../app/documentApiClient/CreateDocumentCapsule.js'
|
|
58
|
+
|
|
59
|
+
describe('CreateDocumentLauncher', () => {
|
|
60
|
+
describe('.get:Payload', () => {
|
|
61
|
+
test('to be CreateDocumentPayload', () => {
|
|
62
|
+
expect(CreateDocumentLauncher.Payload)
|
|
63
|
+
.toBe(CreateDocumentPayload)
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
describe('.get:Capsule', () => {
|
|
68
|
+
test('to be CreateDocumentCapsule', () => {
|
|
69
|
+
expect(CreateDocumentLauncher.Capsule)
|
|
70
|
+
.toBe(CreateDocumentCapsule)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
describe('#launchRequest()', () => {
|
|
75
|
+
const cases = [
|
|
76
|
+
{
|
|
77
|
+
params: {
|
|
78
|
+
pathParameterHash: {
|
|
79
|
+
projectId: 'fake-project-id-001',
|
|
80
|
+
},
|
|
81
|
+
body: {
|
|
82
|
+
title: 'fake-title-001',
|
|
83
|
+
bodyText: 'fake-body-001',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
test.each(cases)('projectId: $params.pathParameterHash.projectId', async ({
|
|
90
|
+
params,
|
|
91
|
+
}) => {
|
|
92
|
+
const responseTally = new Response(
|
|
93
|
+
JSON.stringify({
|
|
94
|
+
document_id: 'fake-document-id-001',
|
|
95
|
+
}),
|
|
96
|
+
{
|
|
97
|
+
status: 200,
|
|
98
|
+
}
|
|
99
|
+
)
|
|
100
|
+
const fetchSpy = jest.fn()
|
|
101
|
+
.mockResolvedValue(responseTally)
|
|
102
|
+
|
|
103
|
+
const SpyLauncher = class extends CreateDocumentLauncher {
|
|
104
|
+
/** @override */
|
|
105
|
+
static get fetch () {
|
|
106
|
+
return fetchSpy
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const launcher = SpyLauncher.create()
|
|
111
|
+
|
|
112
|
+
const payload = SpyLauncher.createPayload(params)
|
|
113
|
+
|
|
114
|
+
const capsule = await launcher.launchRequest({
|
|
115
|
+
payload,
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
expect(fetchSpy)
|
|
119
|
+
.toHaveBeenCalled()
|
|
120
|
+
expect(capsule.extractDocumentId())
|
|
121
|
+
.toBe('fake-document-id-001')
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
})
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
- Build the stub `Response` in **the external API's raw shape** (snake_case `document_id`) and assert all
|
|
128
|
+
the way through that `extractDocumentId()` returns the value in our own terms
|
|
129
|
+
(`fake-document-id-001`). That is how you confirm the whole chain of difference absorption —
|
|
130
|
+
incoming key conversion, then extraction — is working.
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Usage (the implementation steps, and use from the caller)
|
|
2
|
+
|
|
3
|
+
The steps for adding a new API operation, and how a caller such as a resolver or a job (an
|
|
4
|
+
ActionExecuter) uses it. Referenced from [SKILL.md](../SKILL.md). Each layer is detailed in
|
|
5
|
+
[launcher.md](./launcher.md) / [payload.md](./payload.md) / [capsule.md](./capsule.md) /
|
|
6
|
+
[authorization.md](./authorization.md).
|
|
7
|
+
|
|
8
|
+
## Implementation steps
|
|
9
|
+
|
|
10
|
+
Start from **step 0** when the external service itself does not exist yet, or from **step 1** when
|
|
11
|
+
adding an operation to a service that does.
|
|
12
|
+
|
|
13
|
+
### Step 0: lay the groundwork for the service (once per service)
|
|
14
|
+
|
|
15
|
+
1. Create the `app/<serviceName>Client/` directory.
|
|
16
|
+
2. Create the **base launcher** (`Base<Service>Launcher.js`): extend `BaseLauncher` and implement
|
|
17
|
+
`clientConfig`, taking the base URL from an environment variable
|
|
18
|
+
([launcher.md](./launcher.md#base-launcher-one-per-service-holding-clientconfig)).
|
|
19
|
+
3. Add the environment variables for the base URL, credentials and so on to `.env.*`
|
|
20
|
+
(e.g. `DOCUMENT_API_BASE_URL`, `DOCUMENT_API_TOKEN`).
|
|
21
|
+
|
|
22
|
+
### Step 1: read the API spec
|
|
23
|
+
|
|
24
|
+
Establish the endpoint (method and path), the required and optional parameters (path / query / body),
|
|
25
|
+
the response shape, the authentication scheme and the content type. **The differences you find here are
|
|
26
|
+
exactly what gets confined to `Payload` / `Capsule`.**
|
|
27
|
+
|
|
28
|
+
### Step 2: write the payload ([payload.md](./payload.md))
|
|
29
|
+
|
|
30
|
+
1. Implement `method` and `pathname` (variable parts as `[placeholder]`), plus `contentType` for a
|
|
31
|
+
method with a body.
|
|
32
|
+
2. Declare in `querySchema` / `bodySchema` only the keys that need conversion or validation.
|
|
33
|
+
3. When the outgoing keys are snake_case, swap `RequestQueryCtor` / `RequestBodyCtor` for the Snake
|
|
34
|
+
ones.
|
|
35
|
+
4. When authentication is required, implement `AuthorizationBuilderCtor` / `authorizationApiKey`
|
|
36
|
+
([authorization.md](./authorization.md)).
|
|
37
|
+
5. Define the input types with `@typedef`.
|
|
38
|
+
|
|
39
|
+
### Step 3: write the capsule ([capsule.md](./capsule.md))
|
|
40
|
+
|
|
41
|
+
1. When the response is snake_case, swap `ResponseBodyCtor` for `CamelCasedKeyResponseBody`.
|
|
42
|
+
2. Work out which values you need out, and implement `extractXxx()` for each with a null guard
|
|
43
|
+
(`null` / an empty array when missing).
|
|
44
|
+
|
|
45
|
+
### Step 4: write the derived launcher ([launcher.md](./launcher.md#derived-launcher-one-per-api-operation-tying-payload--capsule-together))
|
|
46
|
+
|
|
47
|
+
1. Extend the base launcher.
|
|
48
|
+
2. Return step 2's payload from `static get Payload ()` and step 3's capsule from
|
|
49
|
+
`static get Capsule ()`.
|
|
50
|
+
|
|
51
|
+
### Step 5: write the tests
|
|
52
|
+
|
|
53
|
+
Write unit tests for the `Payload`, the `Capsule` and the `Launcher` ([testing.md](./testing.md)).
|
|
54
|
+
|
|
55
|
+
## Use from the caller (three steps)
|
|
56
|
+
|
|
57
|
+
From a resolver or a job, there are three steps.
|
|
58
|
+
|
|
59
|
+
1. Build the launcher with `Launcher.create()` (the connection settings are assembled from
|
|
60
|
+
`clientConfig`).
|
|
61
|
+
2. Build the payload with `Launcher.createPayload({ ... })` and call
|
|
62
|
+
`launcher.launchRequest({ payload })`.
|
|
63
|
+
3. Check `hasError()` on the returned `Capsule`, and pull values out with `extractXxx()`.
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
import CreateDocumentLauncher from '../../../../../../app/documentApiClient/CreateDocumentLauncher.js'
|
|
67
|
+
|
|
68
|
+
export default class SomeResolver {
|
|
69
|
+
/**
|
|
70
|
+
* Create document launcher (factory for DI).
|
|
71
|
+
*
|
|
72
|
+
* @returns {CreateDocumentLauncher}
|
|
73
|
+
*/
|
|
74
|
+
static createDocumentLauncher () {
|
|
75
|
+
return CreateDocumentLauncher.create()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Create a document via the external API.
|
|
80
|
+
*
|
|
81
|
+
* @param {{
|
|
82
|
+
* projectId: string
|
|
83
|
+
* title: string
|
|
84
|
+
* bodyText: string
|
|
85
|
+
* }} params
|
|
86
|
+
* @returns {Promise<{
|
|
87
|
+
* hasError: boolean
|
|
88
|
+
* documentId: string | null
|
|
89
|
+
* }>}
|
|
90
|
+
*/
|
|
91
|
+
async createDocument ({
|
|
92
|
+
projectId,
|
|
93
|
+
title,
|
|
94
|
+
bodyText,
|
|
95
|
+
}) {
|
|
96
|
+
const payload = CreateDocumentLauncher.createPayload({
|
|
97
|
+
pathParameterHash: {
|
|
98
|
+
projectId,
|
|
99
|
+
},
|
|
100
|
+
body: {
|
|
101
|
+
title,
|
|
102
|
+
bodyText,
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
const capsule = await this.documentApiLauncher.launchRequest({
|
|
107
|
+
payload,
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
hasError: Boolean(capsule.hasError()),
|
|
112
|
+
documentId: capsule.extractDocumentId(),
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
- The caller assembles in camelCase (`bodyText`) and pulls the value out with `extractDocumentId()` —
|
|
119
|
+
nothing more. **It knows nothing of the external API's snake_case or of the response's nesting.** That
|
|
120
|
+
is what confinement looks like when it is working
|
|
121
|
+
([SKILL.md](../SKILL.md#first-principle-confine-every-difference-from-the-external-api-to-payload--capsule-anti-corruption-layer)).
|
|
122
|
+
|
|
123
|
+
## The caller writes no `try-catch`
|
|
124
|
+
|
|
125
|
+
`launchRequest()` catches `fetch` exceptions and parse failures internally and converts them into an
|
|
126
|
+
error `Capsule`, so the caller decides failure with `capsule.hasError()` rather than `try-catch`
|
|
127
|
+
([SKILL.md](../SKILL.md#decide-failure-with-capsulehaserror-not-try-catch)).
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
// Bad: handling errors through two channels, try-catch and the Capsule (a case falls through)
|
|
131
|
+
try {
|
|
132
|
+
const capsule = await launcher.launchRequest({ payload })
|
|
133
|
+
return capsule.extractDocumentId()
|
|
134
|
+
} catch (error) {
|
|
135
|
+
// launchRequest never lands here. This catch is dead
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## DI: inject a stub launcher
|
|
140
|
+
|
|
141
|
+
Let the class receive the launcher through its `constructor` / `create()`, and use a factory method such
|
|
142
|
+
as `create<Service>Launcher()` as the default. A test can then inject a stub launcher.
|
|
143
|
+
|
|
144
|
+
- **Why**: a launcher really does hit the external API, so a test cannot call the real one. With DI
|
|
145
|
+
through a factory, the test gets the stub and production gets the real thing, without the caller
|
|
146
|
+
changing.
|
|
147
|
+
|
|
148
|
+
## Hooks (optional): run something before or after sending
|
|
149
|
+
|
|
150
|
+
Use `launchRequest({ payload, hooks: { beforeRequest, afterRequest } })`. When
|
|
151
|
+
`beforeRequest(payload)` returns `true`, sending is stopped and the aborted-by-hooks `Capsule` is
|
|
152
|
+
returned.
|