diff --git a/docs/LOCAL-CI.md b/docs/LOCAL-CI.md index ff129f80..2cfe3299 100644 --- a/docs/LOCAL-CI.md +++ b/docs/LOCAL-CI.md @@ -22,11 +22,30 @@ system and resource scheduling may differ from GitHub. Missing prerequisites or failed tests do not produce a passing result. `check:all` covers lint, typechecking, the root unit and Node test suites, -and the build. It does not run the `packages/webrtc-experiment` package -self-tests. Playwright runs separately from `check:all`; its default suite -skips farm-view tests unless `FARM_VIEW_E2E=true`. Other browser or E2E -experiments are separate from these checks and may be deferred; this does not -waive any required hosted checks. +and the build. Other browser or E2E experiments are separate from these checks +and may be deferred; this does not waive any required hosted checks. + +The code job leaves two paths out. The default Playwright run skips +`tests/e2e/farm-view.spec.ts`, because the file calls `test.skip` unless +`FARM_VIEW_E2E` is `true`, and the default configuration keeps the farm view +flag off. The dedicated configuration `playwright.farm-view.config.ts` refuses +to load without that variable, turns the flag on and serves the app on port 3101. +Build the app first, then run these cases on their own: + +```sh +FARM_VIEW_E2E=true pnpm exec playwright test --config playwright.farm-view.config.ts +``` + +`packages/webrtc-experiment` has its own typecheck and test scripts, and neither +`check:all` nor the workflow runs them. Run the package scripts yourself: + +```sh +pnpm --dir packages/webrtc-experiment run typecheck +pnpm --dir packages/webrtc-experiment run test +``` + +The transport and interop cases launch Playwright's Chromium. Neither path is a +GitHub check, so a passing code job says nothing about them. Optional container jobs require explicit opt-in: diff --git a/tests/node/telemetry-routes.test.ts b/tests/node/telemetry-routes.test.ts new file mode 100644 index 00000000..81cbd26e --- /dev/null +++ b/tests/node/telemetry-routes.test.ts @@ -0,0 +1,113 @@ +import assert from 'node:assert/strict' +import { IncomingMessage } from 'node:http' +import { registerHooks } from 'node:module' +import { Socket } from 'node:net' +import test, { after } from 'node:test' + +const token = Symbol.for('telemetry-route-test-runtime') +let runtimeCalls = 0 +let handlerCalls = 0 +let enabled = false +const response = { status: 401, body: { error: 'Não autorizado.' } } +Object.defineProperty(globalThis, token, { + configurable: true, + value: () => { + runtimeCalls++ + return enabled + ? async () => { + handlerCalls++ + return response + } + : undefined + }, +}) +const moduleUrl = (source: string) => + `data:text/javascript,${encodeURIComponent(source)}` +const hooks = registerHooks({ + resolve(specifier, context, next) { + if (context.parentURL?.includes('/server/api/connector/telemetry')) { + if (specifier === 'nuxt/server') + return { + url: moduleUrl( + 'export const defineEventHandler = handler => handler', + ), + shortCircuit: true, + } + if (specifier.endsWith('/services/telemetry-http-runtime.ts')) + return { + url: moduleUrl( + "export const telemetryHttpRuntime = () => globalThis[Symbol.for('telemetry-route-test-runtime')]()", + ), + shortCircuit: true, + } + } + return next(specifier, context) + }, +}) +const routes = [ + [ + '/api/connector/telemetry', + (await import('../../apps/farm/server/api/connector/telemetry.post.ts')) + .default, + ], + [ + '/api/connector/telemetry/challenge', + ( + await import('../../apps/farm/server/api/connector/telemetry/challenge.post.ts') + ).default, + ], +] as const +after(() => { + hooks.deregister() + Reflect.deleteProperty(globalThis, token) +}) + +const good = ['content-type', 'application/json'] +for (const [path, route] of routes) { + const run = async (rawHeaders: string[], method = 'POST') => { + const req = new IncomingMessage(new Socket()) + req.method = method + req.url = path + req.rawHeaders = rawHeaders + const event = { + '~app': { node: { req } }, + res: { headers: new Headers(), status: 200 }, + } + runtimeCalls = 0 + handlerCalls = 0 + const body = await route(event as unknown as Parameters[0]) + return { body, event } + } + test(`${path} enforces the boundary before runtime and forwards runtime responses`, async () => { + enabled = true + let r = await run([...good, 'cookie', 'session=synthetic']) + assert.deepEqual(r.body, { error: 'Solicitação inválida.' }) + assert.equal(r.event.res.status, 400) + assert.equal(r.event.res.headers.get('cache-control'), 'no-store') + assert.equal(runtimeCalls, 0) + assert.equal(handlerCalls, 0) + + r = await run(['content-type', 'application/json'], 'GET') + assert.deepEqual(r.body, { error: 'Solicitação inválida.' }) + assert.equal(r.event.res.status, 400) + assert.equal(r.event.res.headers.get('cache-control'), 'no-store') + assert.equal(runtimeCalls, 0) + assert.equal(handlerCalls, 0) + + enabled = false + r = await run(good) + assert.deepEqual(r.body, { error: 'Telemetria indisponível.' }) + assert.equal(r.event.res.status, 503) + assert.equal(r.event.res.headers.get('cache-control'), 'no-store') + assert.equal(runtimeCalls, 1) + assert.equal(handlerCalls, 0) + + enabled = true + r = await run(good) + assert.deepEqual(r.body, response.body) + assert.equal(r.event.res.status, 401) + assert.equal(r.event.res.headers.get('cache-control'), 'no-store') + assert.equal(runtimeCalls, 1) + assert.equal(handlerCalls, 1) + }) +}