|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The declarative-endpoint seam's ABSENCE is announced at `warn` (#5400). |
| 5 | + * |
| 6 | + * ## Why a whole file exists to hold one log level |
| 7 | + * |
| 8 | + * `packages/runtime/src/dispatcher-plugin.ts` mounts declarative `apis:` |
| 9 | + * endpoints through exactly one seam — `IHttpServer.setFallbackHandler`, which |
| 10 | + * is OPTIONAL on the contract and feature-detected with `typeof === 'function'`. |
| 11 | + * On an adapter that omits it there is no second path: every endpoint a stack |
| 12 | + * declared in metadata is unreachable, forever, and the transport answers the |
| 13 | + * bare 404 it would answer for a typo. |
| 14 | + * |
| 15 | + * That line used to be `debug`, and correctly so — while a non-empty `apis:` |
| 16 | + * was rejected WHOLESALE at publish (#4936), no deployment could be missing |
| 17 | + * anything, because no deployment could declare anything. The #5040 E7 publish |
| 18 | + * flip (`packages/spec/src/api/endpoint-publish-gate.ts`, "This module is that |
| 19 | + * flip") ended that premise: declarations publish now and stacks ship them. |
| 20 | + * A `debug` under the default `level: 'info'` is not printed at all |
| 21 | + * (`isEnabled`, `packages/core/src/logger.ts`), so the operator's signal for a |
| 22 | + * silently-dead surface was nothing whatsoever — the exact outcome AGENTS.md's |
| 23 | + * "Absence must be loud" (Route & surface ownership §3) forbids. |
| 24 | + * |
| 25 | + * ## The level is the assertion, so it is welded here |
| 26 | + * |
| 27 | + * A log level is one identifier away from silence and nothing else in the |
| 28 | + * build notices it change. Following #5226's posture — "the level is held by a |
| 29 | + * gate, not by the comment next to it" — these tests fail if the line slides |
| 30 | + * back to `debug` (invisible again) AND if it is escalated to `error` (wrong |
| 31 | + * class). `warn` is what AGENTS.md's "Degradation log levels" question yields: |
| 32 | + * nothing here claims to have PERSISTED anything, so this is a functional |
| 33 | + * degradation — a capability that is not mounted, whose next caller finds out — |
| 34 | + * not a durability one. |
| 35 | + * |
| 36 | + * Reverse verification, direction predicted BEFORE running: restoring |
| 37 | + * `ctx.logger.debug` in the seam-absent branch must turn the level pins RED |
| 38 | + * (this is the ordinary direction — the pins read a predicate on the emitted |
| 39 | + * level, not a count that can pass by producing nothing). Confirmed: with |
| 40 | + * `debug` restored, `emitted at warn` and `never at debug` both fail on the |
| 41 | + * captured line, and the consequence/remedy pins fail with "no warn line". |
| 42 | + * |
| 43 | + * Harness note: the fake server is the `dispatcher-plugin.routes.test.ts` |
| 44 | + * shape. It is the honest one for this branch — the absent member is spelled |
| 45 | + * by simply not being there, which is what the contract tells consumers to |
| 46 | + * probe for, and a real adapter cannot express "I omit this" any better. |
| 47 | + */ |
| 48 | + |
| 49 | +import { describe, it, expect } from 'vitest'; |
| 50 | + |
| 51 | +import { createDispatcherPlugin } from './dispatcher-plugin.js'; |
| 52 | + |
| 53 | +interface LogLine { level: string; message: string; meta?: Record<string, any> } |
| 54 | + |
| 55 | +/** |
| 56 | + * A server WITHOUT `setFallbackHandler` — the member simply absent, the shape |
| 57 | + * the contract documents (`packages/spec/src/contracts/http-server.ts`: "an |
| 58 | + * adapter that cannot express a not-found hook simply omits it"). |
| 59 | + */ |
| 60 | +function makeFallbacklessServer() { |
| 61 | + const noop = () => { /* route registration is not what this file tests */ }; |
| 62 | + return { |
| 63 | + get: noop, post: noop, put: noop, delete: noop, patch: noop, |
| 64 | + } as any; |
| 65 | +} |
| 66 | + |
| 67 | +/** The same server, plus the seam — the control case. */ |
| 68 | +function makeSeamedServer() { |
| 69 | + const server = makeFallbacklessServer(); |
| 70 | + server.setFallbackHandler = () => { /* installed, never invoked here */ }; |
| 71 | + return server; |
| 72 | +} |
| 73 | + |
| 74 | +function makeCtx(fakeServer: any) { |
| 75 | + const logs: LogLine[] = []; |
| 76 | + const kernel = { |
| 77 | + getService: () => undefined, |
| 78 | + getServiceAsync: async () => undefined, |
| 79 | + }; |
| 80 | + const ctx = { |
| 81 | + getKernel: () => kernel, |
| 82 | + getService: (name: string) => (name === 'http.server' ? fakeServer : undefined), |
| 83 | + environmentId: undefined, |
| 84 | + logger: { |
| 85 | + info(message: string, meta?: any) { logs.push({ level: 'info', message, meta }); }, |
| 86 | + warn(message: string, meta?: any) { logs.push({ level: 'warn', message, meta }); }, |
| 87 | + error(message: string, meta?: any) { logs.push({ level: 'error', message, meta }); }, |
| 88 | + debug(message: string, meta?: any) { logs.push({ level: 'debug', message, meta }); }, |
| 89 | + }, |
| 90 | + hook: () => {}, |
| 91 | + on: () => {}, |
| 92 | + } as any; |
| 93 | + return { ctx, logs }; |
| 94 | +} |
| 95 | + |
| 96 | +/** Every line that talks about the missing seam, at whatever level it came out. */ |
| 97 | +const seamLines = (logs: LogLine[]) => logs.filter((l) => l.message.includes('setFallbackHandler')); |
| 98 | + |
| 99 | +async function bootWith(server: any) { |
| 100 | + const { ctx, logs } = makeCtx(server); |
| 101 | + const plugin = createDispatcherPlugin({ prefix: '/api/v1', securityHeaders: false }); |
| 102 | + await plugin.start?.(ctx); |
| 103 | + return logs; |
| 104 | +} |
| 105 | + |
| 106 | +describe('dispatcher declarative-endpoint seam — absence is loud (#5400)', () => { |
| 107 | + it('announces the missing seam at `warn` — never `debug`, never `error`', async () => { |
| 108 | + const logs = await bootWith(makeFallbacklessServer()); |
| 109 | + |
| 110 | + const lines = seamLines(logs); |
| 111 | + // Said ONCE, at boot, not once per anything. |
| 112 | + expect(lines).toHaveLength(1); |
| 113 | + // The level IS the fix. `debug` is the pre-#5400 state and is invisible |
| 114 | + // under the default `info`; `error` is the over-escalation AGENTS.md's |
| 115 | + // durability question rules out (nothing here claims persistence). |
| 116 | + expect(lines[0].level).toBe('warn'); |
| 117 | + expect(logs.filter((l) => l.level === 'debug' && l.message.includes('setFallbackHandler'))).toEqual([]); |
| 118 | + expect(logs.filter((l) => l.level === 'error')).toEqual([]); |
| 119 | + }, 60_000); |
| 120 | + |
| 121 | + it('names the CONSEQUENCE: declared endpoints are unreachable and answer a bare 404', async () => { |
| 122 | + const logs = await bootWith(makeFallbacklessServer()); |
| 123 | + |
| 124 | + const line = seamLines(logs).find((l) => l.level === 'warn'); |
| 125 | + expect(line).toBeDefined(); |
| 126 | + const msg = line!.message; |
| 127 | + // What is lost: not "some routes", but every endpoint declared in |
| 128 | + // metadata, on this transport. |
| 129 | + expect(msg).toMatch(/metadata-declared/); |
| 130 | + expect(msg).toMatch(/`apis:`/); |
| 131 | + expect(msg).toMatch(/UNREACHABLE/); |
| 132 | + // And what the caller sees instead — the bare 404 that reads like a |
| 133 | + // typo and sends operators hunting in the wrong place. |
| 134 | + expect(msg).toMatch(/bare 404/); |
| 135 | + }, 60_000); |
| 136 | + |
| 137 | + it('names the REMEDY: compose an adapter that implements the seam', async () => { |
| 138 | + const logs = await bootWith(makeFallbacklessServer()); |
| 139 | + |
| 140 | + const msg = seamLines(logs).find((l) => l.level === 'warn')!.message; |
| 141 | + // The member to implement... |
| 142 | + expect(msg).toMatch(/setFallbackHandler/); |
| 143 | + // ...and a concrete adapter that already does, so the remedy is |
| 144 | + // actionable without reading the contract first. |
| 145 | + expect(msg).toMatch(/@objectstack\/plugin-hono-server/); |
| 146 | + }, 60_000); |
| 147 | + |
| 148 | + it('carries the affected mount prefix as structured meta', async () => { |
| 149 | + const logs = await bootWith(makeFallbacklessServer()); |
| 150 | + |
| 151 | + const line = seamLines(logs).find((l) => l.level === 'warn')!; |
| 152 | + expect(line.meta).toMatchObject({ |
| 153 | + mount: '/api/v1/apps/', |
| 154 | + declarativeEndpoints: 'unreachable', |
| 155 | + }); |
| 156 | + }, 60_000); |
| 157 | + |
| 158 | + it('stays SILENT when the adapter does expose the seam', async () => { |
| 159 | + const logs = await bootWith(makeSeamedServer()); |
| 160 | + |
| 161 | + // The counter-case that keeps the warn a signal instead of boot noise: |
| 162 | + // on a conforming adapter nothing is missing, so nothing is announced. |
| 163 | + expect(seamLines(logs)).toEqual([]); |
| 164 | + // And the positive line is the one that gets printed instead. |
| 165 | + expect(logs.some((l) => l.level === 'info' && l.message.includes('Declarative endpoint dispatch step armed'))) |
| 166 | + .toBe(true); |
| 167 | + }, 60_000); |
| 168 | +}); |
0 commit comments