|
20 | 20 | * are the tests that fail if the contract moves. |
21 | 21 | */ |
22 | 22 |
|
23 | | -import { describe, it, expect } from 'vitest'; |
| 23 | +import { describe, it, expect, vi } from 'vitest'; |
24 | 24 | import { ValidationError } from '@objectstack/objectql'; |
25 | 25 |
|
26 | 26 | import { HttpDispatcher } from './http-dispatcher.js'; |
@@ -100,7 +100,9 @@ async function analyticsQuery(thrown: unknown) { |
100 | 100 | header() { return res; }, |
101 | 101 | json(b: any) { res.body = b; return res; }, |
102 | 102 | }; |
103 | | - await handlers['POST /api/v1/analytics/query']({ body: { cube: 'x', query: {} }, query: {} }, res); |
| 103 | + // [#3878] Body must pass entry validation so the SERVICE's thrown error — |
| 104 | + // the thing under test — is what reaches the exit, not an entry 400. |
| 105 | + await handlers['POST /api/v1/analytics/query']({ body: { cube: 'x', measures: ['count'] }, query: {} }, res); |
104 | 106 | return res; |
105 | 107 | } |
106 | 108 |
|
@@ -138,3 +140,58 @@ describe('#3918 — both exits serve the real ValidationError as 400 + fields[]' |
138 | 140 | expect(res.body.error.details.fields).toEqual([]); |
139 | 141 | }); |
140 | 142 | }); |
| 143 | + |
| 144 | +// --------------------------------------------------------------------------- |
| 145 | + |
| 146 | +/** [#3878] Post `body` through the real route handler; the service never throws. */ |
| 147 | +async function postAnalyticsBody(body: unknown) { |
| 148 | + const handlers: Record<string, (req: any, res: any) => any> = {}; |
| 149 | + const rec = (verb: string) => (path: string, h: any) => { handlers[`${verb} ${path}`] = h; }; |
| 150 | + const server = { |
| 151 | + get: rec('GET'), post: rec('POST'), put: rec('PUT'), |
| 152 | + delete: rec('DELETE'), patch: rec('PATCH'), |
| 153 | + }; |
| 154 | + const query = vi.fn(async () => ({ rows: [] })); |
| 155 | + const analytics = { query, getMeta: async () => ({ cubes: [] }), generateSql: async () => ({ sql: null }) }; |
| 156 | + const kernel = { |
| 157 | + getService: (n: string) => (n === 'analytics' ? analytics : undefined), |
| 158 | + getServiceAsync: async (n: string) => (n === 'analytics' ? analytics : undefined), |
| 159 | + }; |
| 160 | + const plugin = createDispatcherPlugin({ prefix: '/api/v1', securityHeaders: false }); |
| 161 | + await plugin.start?.({ |
| 162 | + getKernel: () => kernel, |
| 163 | + getService: (n: string) => (n === 'http.server' ? server : undefined), |
| 164 | + environmentId: undefined, |
| 165 | + logger: { info() {}, warn() {}, error() {}, debug() {} }, |
| 166 | + hook: () => {}, on: () => {}, |
| 167 | + } as any); |
| 168 | + |
| 169 | + const res: any = { |
| 170 | + statusCode: undefined, body: undefined, |
| 171 | + status(c: number) { res.statusCode = c; return res; }, |
| 172 | + header() { return res; }, |
| 173 | + json(b: any) { res.body = b; return res; }, |
| 174 | + }; |
| 175 | + await handlers['POST /api/v1/analytics/query']({ body, query: {} }, res); |
| 176 | + return { res, query }; |
| 177 | +} |
| 178 | + |
| 179 | +describe('#3878 — entry validation reaches the wire as a 400, service untouched', () => { |
| 180 | + it('the retired envelope answers 400 with the tombstone prescription', async () => { |
| 181 | + const { res, query } = await postAnalyticsBody({ cube: 'x', query: { measures: ['count'] } }); |
| 182 | + |
| 183 | + expect(res.statusCode).toBe(400); |
| 184 | + expect(res.body.error.code).toBe('VALIDATION_FAILED'); |
| 185 | + expect(res.body.error.message).toContain('top level'); |
| 186 | + expect(query).not.toHaveBeenCalled(); |
| 187 | + // A caller mistake, not a server fault: the reporter side-channel stays clear. |
| 188 | + expect(res.__obsRecordedError).toBeUndefined(); |
| 189 | + }); |
| 190 | + |
| 191 | + it('a valid bare body passes through and answers 200', async () => { |
| 192 | + const { res, query } = await postAnalyticsBody({ cube: 'x', measures: ['count'] }); |
| 193 | + |
| 194 | + expect(res.statusCode).toBe(200); |
| 195 | + expect(query).toHaveBeenCalledOnce(); |
| 196 | + }); |
| 197 | +}); |
0 commit comments