const fs = require("fs"); const path = require("path"); const vm = require("vm"); const assert = require("assert"); const rootDir = path.resolve(__dirname, "..", ".."); const loadFileClientModule = (injected = {}) => { const filePath = path.join(rootDir, "actions", "clients", "fileClient.js"); let source = fs.readFileSync(filePath, "utf8"); source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, ""); source = source.replace(/export const\s+/g, "const "); source += "\nmodule.exports = { getFileJson, getSignedFileJson, downloadFileBlob, postSignedFileJson };\n"; const context = { module: { exports: {} }, exports: {}, require, getJson: async () => { throw new Error("getJson not injected"); }, requestJson: async () => { throw new Error("requestJson not injected"); }, getSignedJson: async () => { throw new Error("getSignedJson not injected"); }, postSignedJson: async () => { throw new Error("postSignedJson not injected"); }, ...injected }; vm.runInNewContext(source, context, { filename: filePath }); return context.module.exports; }; const tests = []; const test = (name, fn) => tests.push({ name, fn }); test("clients/fileClient getFileJson delegates to getJson", async () => { const calls = []; const mod = loadFileClientModule({ getJson: async (url) => { calls.push(url); return { ok: true }; } }); const result = await mod.getFileJson("/api/file/getbloblistproxy?x=1"); assert.deepStrictEqual(JSON.parse(JSON.stringify(result)), { ok: true }); assert.strictEqual(calls.length, 1); assert.strictEqual(calls[0], "/api/file/getbloblistproxy?x=1"); }); test("clients/fileClient getSignedFileJson signs url and requests json", async () => { const signedCalls = []; const mod = loadFileClientModule({ getSignedJson: async (queryUrl) => { signedCalls.push(queryUrl); return { deleted: true }; } }); const result = await mod.getSignedFileJson( "/api/file/deleteblobcase?container=a&casefolderID=b" ); assert.deepStrictEqual(JSON.parse(JSON.stringify(result)), { deleted: true }); assert.strictEqual(signedCalls.length, 1); assert.strictEqual( signedCalls[0], "/api/file/deleteblobcase?container=a&casefolderID=b" ); }); test("clients/fileClient downloadFileBlob requests blob response", async () => { const requestCalls = []; const mod = loadFileClientModule({ requestJson: async (config) => { requestCalls.push(config); return "blob-data"; } }); const result = await mod.downloadFileBlob("/api/file/downloadblob?x=1"); assert.strictEqual(result, "blob-data"); assert.strictEqual(requestCalls.length, 1); assert.strictEqual(requestCalls[0].method, "get"); assert.strictEqual(requestCalls[0].responseType, "blob"); assert.strictEqual(requestCalls[0].url, "/api/file/downloadblob?x=1"); }); test("clients/fileClient postSignedFileJson signs url and posts payload", async () => { const signedCalls = []; const postedCalls = []; const mod = loadFileClientModule({ postSignedJson: async (queryUrl, data, config) => { signedCalls.push(queryUrl); postedCalls.push({ data, config }); return { uploaded: true }; } }); const payload = { name: "doc" }; const result = await mod.postSignedFileJson( "/api/file/uploadsinglefile", payload, { headers: { "content-type": "multipart/form-data" } } ); assert.deepStrictEqual(JSON.parse(JSON.stringify(result)), { uploaded: true }); assert.strictEqual(signedCalls.length, 1); assert.strictEqual(postedCalls.length, 1); assert.strictEqual(signedCalls[0], "/api/file/uploadsinglefile"); assert.deepStrictEqual( JSON.parse(JSON.stringify(postedCalls[0].data)), payload ); assert.strictEqual( postedCalls[0].config.headers["content-type"], "multipart/form-data" ); }); const run = async () => { let passed = 0; for (const currentTest of tests) { await currentTest.fn(); passed += 1; } console.log( `Phase 22 file-client tests passed (${passed}/${tests.length}).` ); }; module.exports = run; if (require.main === module) { run().catch((error) => { console.error(error); process.exit(1); }); }