|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 4 | +import { |
| 5 | + resolveShowcaseSelfUrl, |
| 6 | + SHOWCASE_DEFAULT_SELF_URL, |
| 7 | + SHOWCASE_DEFAULT_PORT, |
| 8 | +} from '../src/system/self-url.js'; |
| 9 | + |
| 10 | +/** |
| 11 | + * #7538 — the showcase's self-pointing connectors must follow the port the |
| 12 | + * instance actually bound. |
| 13 | + * |
| 14 | + * Before the fix, `StatusApiConnector` and `StatusOpenApiConnector` carried the |
| 15 | + * literal `http://127.0.0.1:3000` in `providerConfig`, so every flow that |
| 16 | + * dispatched through them failed with `fetch failed` on any instance not |
| 17 | + * listening on 3000 — CI, QA, any dev boot on an isolated port. The failure is |
| 18 | + * indistinguishable from a sandbox egress block, which is what made it |
| 19 | + * expensive to diagnose (the QA run in #7516 needed a TCP forwarder on 3000 to |
| 20 | + * prove the address was the whole problem). |
| 21 | + * |
| 22 | + * The guards below pin both halves of the contract the fix must hold: |
| 23 | + * a non-3000 environment moves the connectors' `baseUrl`, and an empty |
| 24 | + * environment still resolves to the historical literal. |
| 25 | + * |
| 26 | + * Reverse verification (expected direction: RED on revert). Restoring the |
| 27 | + * literal at src/system/connectors/index.ts:57 / :89 turns the two |
| 28 | + * "follows the environment" cases below red while the two "defaults" cases stay |
| 29 | + * green — a literal is, by construction, still correct in the default case. |
| 30 | + * That asymmetry is the point: the default-case assertions alone can never |
| 31 | + * detect the bug, so both halves are required. |
| 32 | + */ |
| 33 | + |
| 34 | +// Ambient `process` with `env` — test/node-shim.d.ts declares the global as |
| 35 | +// `{ cwd(): string }` only, and this module-scoped declaration shadows it |
| 36 | +// rather than widening the shared shim (the same idiom objectstack.config.ts |
| 37 | +// uses for its own env reads). |
| 38 | +declare const process: { env: Record<string, string | undefined> }; |
| 39 | + |
| 40 | +/** Load the connector metadata fresh under a given environment. */ |
| 41 | +async function connectorsUnderEnv(env: Record<string, string | undefined>) { |
| 42 | + vi.resetModules(); |
| 43 | + const saved: Record<string, string | undefined> = {}; |
| 44 | + for (const key of ['SHOWCASE_SELF_URL', 'OS_PORT', 'PORT']) { |
| 45 | + saved[key] = process.env[key]; |
| 46 | + if (env[key] === undefined) delete process.env[key]; |
| 47 | + else process.env[key] = env[key]; |
| 48 | + } |
| 49 | + try { |
| 50 | + return await import('../src/system/connectors/index.js'); |
| 51 | + } finally { |
| 52 | + for (const [key, value] of Object.entries(saved)) { |
| 53 | + if (value === undefined) delete process.env[key]; |
| 54 | + else process.env[key] = value; |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function baseUrlOf(connector: { providerConfig?: Record<string, unknown> }): unknown { |
| 60 | + return connector.providerConfig?.baseUrl; |
| 61 | +} |
| 62 | + |
| 63 | +afterEach(() => { |
| 64 | + vi.resetModules(); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('resolveShowcaseSelfUrl precedence (#7538)', () => { |
| 68 | + it('prefers an explicit SHOWCASE_SELF_URL over any port', () => { |
| 69 | + expect(resolveShowcaseSelfUrl({ SHOWCASE_SELF_URL: 'https://showcase.internal', OS_PORT: '4711' })) |
| 70 | + .toBe('https://showcase.internal'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('trims a trailing slash so callers can join paths without doubling it', () => { |
| 74 | + expect(resolveShowcaseSelfUrl({ SHOWCASE_SELF_URL: 'http://127.0.0.1:8080/' })) |
| 75 | + .toBe('http://127.0.0.1:8080'); |
| 76 | + }); |
| 77 | + |
| 78 | + it("follows the CLI's own OS_PORT when no explicit URL is set", () => { |
| 79 | + expect(resolveShowcaseSelfUrl({ OS_PORT: '4711' })).toBe('http://127.0.0.1:4711'); |
| 80 | + }); |
| 81 | + |
| 82 | + it("follows the CLI's deprecated PORT alias when OS_PORT is absent", () => { |
| 83 | + expect(resolveShowcaseSelfUrl({ PORT: '5822' })).toBe('http://127.0.0.1:5822'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('prefers OS_PORT over PORT — the same order readEnvWithDeprecation uses', () => { |
| 87 | + expect(resolveShowcaseSelfUrl({ OS_PORT: '4711', PORT: '5822' })).toBe('http://127.0.0.1:4711'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('falls back to the historical literal on an empty environment', () => { |
| 91 | + expect(resolveShowcaseSelfUrl({})).toBe('http://127.0.0.1:3000'); |
| 92 | + expect(SHOWCASE_DEFAULT_SELF_URL).toBe('http://127.0.0.1:3000'); |
| 93 | + expect(SHOWCASE_DEFAULT_PORT).toBe('3000'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('ignores a blank value rather than resolving to a broken URL', () => { |
| 97 | + expect(resolveShowcaseSelfUrl({ SHOWCASE_SELF_URL: ' ', OS_PORT: ' ' })) |
| 98 | + .toBe('http://127.0.0.1:3000'); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('self-pointing connector instances follow the environment (#7538)', () => { |
| 103 | + it('StatusApiConnector resolves against a non-3000 port', async () => { |
| 104 | + const mod = await connectorsUnderEnv({ OS_PORT: '4711' }); |
| 105 | + expect(baseUrlOf(mod.StatusApiConnector)).toBe('http://127.0.0.1:4711'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('StatusOpenApiConnector resolves against a non-3000 port', async () => { |
| 109 | + const mod = await connectorsUnderEnv({ OS_PORT: '4711' }); |
| 110 | + expect(baseUrlOf(mod.StatusOpenApiConnector)).toBe('http://127.0.0.1:4711'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('both instances honour an explicit SHOWCASE_SELF_URL', async () => { |
| 114 | + const mod = await connectorsUnderEnv({ SHOWCASE_SELF_URL: 'http://127.0.0.1:8123' }); |
| 115 | + expect(baseUrlOf(mod.StatusApiConnector)).toBe('http://127.0.0.1:8123'); |
| 116 | + expect(baseUrlOf(mod.StatusOpenApiConnector)).toBe('http://127.0.0.1:8123'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('both instances still default to 127.0.0.1:3000 with nothing set', async () => { |
| 120 | + const mod = await connectorsUnderEnv({}); |
| 121 | + expect(baseUrlOf(mod.StatusApiConnector)).toBe('http://127.0.0.1:3000'); |
| 122 | + expect(baseUrlOf(mod.StatusOpenApiConnector)).toBe('http://127.0.0.1:3000'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('no connector carries a hard-wired self URL any more', async () => { |
| 126 | + const mod = await connectorsUnderEnv({ OS_PORT: '4711' }); |
| 127 | + const hardWired = (mod.allConnectors as Array<{ name: string; providerConfig?: Record<string, unknown> }>) |
| 128 | + .filter((c) => c.providerConfig?.baseUrl === 'http://127.0.0.1:3000') |
| 129 | + .map((c) => c.name); |
| 130 | + expect(hardWired).toEqual([]); |
| 131 | + }); |
| 132 | +}); |
0 commit comments