const { strict: assert } = require('node:assert'); const { app } = require('egg-mock/bootstrap'); describe('test/app/controller/api_v2.test.js', () => { before(() => { // avoid database or factory initialization during tests app.model = {}; }); it('dispatches a simple method and returns payload', async () => { // mock dispatcher.invoke so we don't need real services or auth app.mockService('dispatcher', 'invoke', async (method, data, auth, ctx) => { // verify parameters arrived as expected assert(method === 'index.fastWhereTree'); assert.deepEqual(data, { type: 'example' }); // return a fake successful response return { isLog: true, result: { status: 200, data: { foo: 'bar', method } } }; }); const res = await app.httpRequest() .post('/api/v2?method=index.fastWhereTree&v=1.0&format=json×tamp=123') .set('Content-Type', 'application/json') .send({ type: 'example' }) .expect(200); assert.deepEqual(res.body, { foo: 'bar', method: 'index.fastWhereTree' }); }); it('returns 401 when dispatcher.invoke throws (simulating auth failure)', async () => { // simulate auth failure by making invoke return unauthorized app.mockService('dispatcher', 'invoke', async () => { return { isLog: false, result: { status: 401, message: 'Unauthorized' } }; }); const res = await app.httpRequest() .post('/api/v2?method=index.fastWhereTree&v=1.0&format=json×tamp=123') .set('Content-Type', 'application/json') .send({}) .expect(401); // controller forwards status from dispatcher assert(res.body && res.body.message === 'Unauthorized'); }); });