|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #10993 — `version` on `GET /health` and in the discovery payload |
| 5 | + * (`registerBuiltinDomains()` / `getDiscoveryInfo()` in `./http-dispatcher.ts`) |
| 6 | + * must be DERIVED: an injected `OS_RUNTIME_VERSION` stamp, falling back to |
| 7 | + * the resolved `@objectstack/runtime` package version — never the `'1.0.0'` |
| 8 | + * literal both sites hardcoded before this fix. |
| 9 | + * |
| 10 | + * Every assertion here drives the REAL `HttpDispatcher.dispatch()` path |
| 11 | + * (never the source text or `resolveRuntimeVersion()` in isolation), so it |
| 12 | + * fails if either handler stops actually reading the derived value — the |
| 13 | + * anti-vacuity requirement #10993 calls out by name. A reverse-verification |
| 14 | + * pass (temporarily restoring the `'1.0.0'` literal on both sites) confirmed |
| 15 | + * this file goes red at exactly that restoration and names the failing site |
| 16 | + * — see the PR description for the transcript; that step is deliberately |
| 17 | + * NOT encoded as a test here, since a test cannot un-revert itself mid-run. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 21 | +import { createRequire } from 'node:module'; |
| 22 | +import { HttpDispatcher } from './http-dispatcher'; |
| 23 | +import { ObjectKernel } from '@objectstack/core'; |
| 24 | + |
| 25 | +/** |
| 26 | + * The real fallback value, read the same way `resolveRuntimeVersion()` reads |
| 27 | + * it — so this assertion tracks the production code instead of independently |
| 28 | + * guessing a version string that could drift from it. |
| 29 | + */ |
| 30 | +const RUNTIME_PACKAGE_VERSION = ( |
| 31 | + createRequire(import.meta.url)('../package.json') as { version: string } |
| 32 | +).version; |
| 33 | + |
| 34 | +function newDispatcher(): HttpDispatcher { |
| 35 | + const kernel = { |
| 36 | + services: {}, |
| 37 | + context: { getService: vi.fn() }, |
| 38 | + } as unknown as ObjectKernel; |
| 39 | + return new HttpDispatcher(kernel); |
| 40 | +} |
| 41 | + |
| 42 | +async function healthVersion(dispatcher: HttpDispatcher): Promise<unknown> { |
| 43 | + const res = await dispatcher.dispatch('GET', '/health', undefined, undefined, {} as any); |
| 44 | + return res.response?.body?.data?.version; |
| 45 | +} |
| 46 | + |
| 47 | +async function discoveryVersion(dispatcher: HttpDispatcher): Promise<unknown> { |
| 48 | + const res = await dispatcher.dispatch('GET', '/discovery', undefined, {}, {} as any); |
| 49 | + return res.response?.body?.data?.version; |
| 50 | +} |
| 51 | + |
| 52 | +describe('HttpDispatcher — served `version` is derived, not a literal (#10993)', () => { |
| 53 | + const ORIGINAL_STAMP = process.env.OS_RUNTIME_VERSION; |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + if (ORIGINAL_STAMP === undefined) delete process.env.OS_RUNTIME_VERSION; |
| 57 | + else process.env.OS_RUNTIME_VERSION = ORIGINAL_STAMP; |
| 58 | + }); |
| 59 | + |
| 60 | + // A value with no plausible relationship to '1.0.0' or the package |
| 61 | + // version — if either handler answered anything else, it would prove |
| 62 | + // the handler is not actually reading the injected stamp. |
| 63 | + const INJECTED_STAMP = 'stamp-9f3c7a1-cloud1537-could-not-be-a-coincidence'; |
| 64 | + |
| 65 | + it('serves the injected OS_RUNTIME_VERSION stamp verbatim on /health', async () => { |
| 66 | + process.env.OS_RUNTIME_VERSION = INJECTED_STAMP; |
| 67 | + const dispatcher = newDispatcher(); |
| 68 | + |
| 69 | + expect(await healthVersion(dispatcher)).toBe(INJECTED_STAMP); |
| 70 | + }); |
| 71 | + |
| 72 | + it('serves the SAME injected stamp on the discovery payload — one derived source, two callers', async () => { |
| 73 | + process.env.OS_RUNTIME_VERSION = INJECTED_STAMP; |
| 74 | + const dispatcher = newDispatcher(); |
| 75 | + |
| 76 | + expect(await discoveryVersion(dispatcher)).toBe(INJECTED_STAMP); |
| 77 | + }); |
| 78 | + |
| 79 | + it('falls back to the resolved package version — not a literal, not undefined — when no stamp is injected', async () => { |
| 80 | + delete process.env.OS_RUNTIME_VERSION; |
| 81 | + const dispatcher = newDispatcher(); |
| 82 | + |
| 83 | + const health = await healthVersion(dispatcher); |
| 84 | + const discovery = await discoveryVersion(dispatcher); |
| 85 | + |
| 86 | + expect(health).toBe(RUNTIME_PACKAGE_VERSION); |
| 87 | + expect(discovery).toBe(RUNTIME_PACKAGE_VERSION); |
| 88 | + expect(health).not.toBe('1.0.0'); |
| 89 | + expect(health).not.toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('resolves the stamp/fallback ONCE at construction — a later env change does not retroactively affect an already-constructed dispatcher', async () => { |
| 93 | + delete process.env.OS_RUNTIME_VERSION; |
| 94 | + const dispatcher = newDispatcher(); |
| 95 | + expect(await healthVersion(dispatcher)).toBe(RUNTIME_PACKAGE_VERSION); |
| 96 | + |
| 97 | + process.env.OS_RUNTIME_VERSION = 'late-stamp-must-not-apply-to-the-existing-instance'; |
| 98 | + expect(await healthVersion(dispatcher)).toBe(RUNTIME_PACKAGE_VERSION); |
| 99 | + |
| 100 | + // A freshly constructed dispatcher DOES pick up the now-set stamp — |
| 101 | + // confirms the previous assertion is about construction-time |
| 102 | + // resolution, not about the stamp never being read at all. |
| 103 | + expect(await healthVersion(newDispatcher())).toBe('late-stamp-must-not-apply-to-the-existing-instance'); |
| 104 | + }); |
| 105 | +}); |
0 commit comments