test(phase7): align service behaviour harness with client wrappers

This commit is contained in:
2026-03-25 10:04:16 +00:00
parent 5309ffb8cf
commit 480897bdc0
2 changed files with 71 additions and 4 deletions
+41 -4
View File
@@ -72,6 +72,38 @@ const loadServiceModule = (fileName, injected = {}) => {
source = source.replace(/export const\s+/g, "const ");
source += `\nmodule.exports = { ${exportNames.join(", ")} };\n`;
const defaultGetJson = (url, config) => {
if (!injected.axios || !injected.axios.get) {
throw new Error("Missing axios.get for default getJson mock");
}
return injected.axios
.get(url, config)
.then((response) => response.data);
};
const defaultRequestJson = (config) => {
if (!injected.axios) {
throw new Error("Missing axios for default requestJson mock");
}
return injected.axios(config).then((response) => response.data);
};
const defaultBuildHashedQueryUrl = async (queryUrl) => {
if (!injected.axios || !injected.axios.get) {
throw new Error(
"Missing axios.get for default buildHashedQueryUrl mock"
);
}
const hashResponse = await injected.axios.get(
"/api/endpoint/gethash_api?path=" + encodeURIComponent(queryUrl)
);
return queryUrl + hashResponse.data.hash;
};
const context = {
module: { exports: {} },
exports: {},
@@ -85,6 +117,10 @@ const loadServiceModule = (fileName, injected = {}) => {
warn: () => {},
error: () => {}
},
getJson: injected.getJson || defaultGetJson,
requestJson: injected.requestJson || defaultRequestJson,
buildHashedQueryUrl:
injected.buildHashedQueryUrl || defaultBuildHashedQueryUrl,
...injected
};
@@ -367,7 +403,7 @@ test("notify/sendEmail posts payload and returns response data", async () => {
const axios = createAxiosMock();
const logger = createLoggerMock();
axios.postHandler = async () => ({ data: { id: "msg-1" } });
axios.requestHandler = async () => ({ data: { id: "msg-1" } });
const notify = loadServiceModule("notifyDirectService.js", {
axios,
@@ -382,8 +418,9 @@ test("notify/sendEmail posts payload and returns response data", async () => {
);
assert.deepStrictEqual(normalize(result), { id: "msg-1" });
assert.strictEqual(axios.calls[0].url, "/api/email/notify");
assert.deepStrictEqual(normalize(axios.calls[0].data), {
assert.strictEqual(axios.calls[0].config.url, "/api/email/notify");
assert.strictEqual(axios.calls[0].config.method, "post");
assert.deepStrictEqual(normalize(axios.calls[0].config.data), {
templateId: "template-1",
emailAddress: "person@example.com",
reference: "ref-123",
@@ -396,7 +433,7 @@ test("notify/sendEmail logs and rethrows on failure", async () => {
const logger = createLoggerMock();
const error = createAxiosError(429, "Too Many Requests");
axios.postHandler = async () => Promise.reject(error);
axios.requestHandler = async () => Promise.reject(error);
const notify = loadServiceModule("notifyDirectService.js", {
axios,