import { describe, expect, it } from 'vitest' import { buildRuntimeReadinessReport, deriveHealthBaseUrl, fetchLiveAuthHealth, fetchLiveReleaseManifest, fetchLiveWebsiteShell, parseEnvFile, } from './runtime-readiness-lib.mjs' function createMockResponse({ ok = true, status = 200, contentType = 'application/json', body = '', } = {}) { return { ok, status, headers: { get(name) { if (String(name).toLowerCase() === 'content-type') { return contentType } return null }, }, async text() { return body }, } } describe('parseEnvFile', () => { it('parses simple dotenv-style content with quotes and export prefixes', () => { const parsed = parseEnvFile(` # comment export API_DOMAIN="https://hypertwist.app" COOKIE_SECURE=true VITE_SUPPORT_EMAIL='hello@hypertwist.app' `) expect(parsed).toEqual({ API_DOMAIN: 'https://hypertwist.app', COOKIE_SECURE: 'true', VITE_SUPPORT_EMAIL: 'hello@hypertwist.app', }) }) }) describe('live deployment fetch helpers', () => { it('rejects placeholder HTML from the auth-health route with an explicit error', async () => { await expect( fetchLiveAuthHealth('https://hypertwist.app', async () => createMockResponse({ contentType: 'text/html; charset=utf-8', body: ` HyperTwist

Deployment target is live on the new VPS. Application rollout is pending.

`, })), ).rejects.toThrow(/placeholder rollout page/i) }) it('parses a valid anonymous public release manifest', async () => { const payload = await fetchLiveReleaseManifest('https://hypertwist.app', async () => createMockResponse({ body: JSON.stringify({ ok: true, manifest: { viewer: { authenticated: false, canDownload: false, }, platforms: [ { platform_key: 'windows', configured: true, download_url: null, }, ], }, }), })) expect(payload.manifest.viewer.authenticated).toBe(false) expect(payload.manifest.platforms[0].platform_key).toBe('windows') }) it('detects the first-party shell marker and placeholder posture on the website root', async () => { const shell = await fetchLiveWebsiteShell('https://hypertwist.app', async () => createMockResponse({ contentType: 'text/html; charset=utf-8', body: ` HyperTwist
`, })) expect(shell.title).toBe('HyperTwist') expect(shell.hasShellMarker).toBe(true) expect(shell.hasRootMount).toBe(true) expect(shell.placeholderDetected).toBe(false) }) }) describe('buildRuntimeReadinessReport', () => { it('passes a fully configured public same-origin posture', () => { const report = buildRuntimeReadinessReport({ frontendEnv: { VITE_SUPERTOKENS_API_DOMAIN: 'https://hypertwist.app', VITE_SUPERTOKENS_WEBSITE_DOMAIN: 'https://hypertwist.app', VITE_AUTH_API_BASE_URL: 'https://hypertwist.app', VITE_WINDOWS_DOWNLOAD_URL: 'https://downloads.hypertwist.app/windows.exe', VITE_PADDLE_CHECKOUT_URL_OPERATOR: 'https://buy.paddle.com/operator', VITE_PADDLE_CHECKOUT_URL_STUDIO: 'https://buy.paddle.com/studio', VITE_MPL_SOURCE_URL: 'https://hypertwist.app/open-source/source', VITE_OPEN_SOURCE_REPO_URL: 'https://git.scriptoriumai.io/scriptoriumadmin/hypertwist', }, serverEnv: { API_DOMAIN: 'https://hypertwist.app', WEBSITE_DOMAIN: 'https://hypertwist.app', SUPERTOKENS_CORE_URI: 'https://auth-core.internal', COOKIE_SECURE: 'true', SERVE_STATIC_WEBSITE: 'true', PADDLE_WEBHOOK_SECRET: 'secret', PADDLE_PRICE_PLAN_MAP: '{"pri_operator":"operator"}', }, liveHealth: { supertokens: { ready: true }, fallback: { active: false }, runtime: { public_origin_ready: true, mode: 'public', errors: [], warnings: [], }, billing: { webhookSecretConfigured: true, productPlanMapConfigured: false, pricePlanMapConfigured: true, }, }, }) expect(report.ok).toBe(true) expect(report.failures).toEqual([]) }) it('warns when same-origin public posture leaves static website serving ambiguous', () => { const report = buildRuntimeReadinessReport({ frontendEnv: { VITE_SUPERTOKENS_API_DOMAIN: 'https://hypertwist.app', VITE_SUPERTOKENS_WEBSITE_DOMAIN: 'https://hypertwist.app', VITE_AUTH_API_BASE_URL: 'https://hypertwist.app', VITE_WINDOWS_DOWNLOAD_URL: 'https://downloads.hypertwist.app/windows.exe', VITE_PADDLE_CHECKOUT_URL_OPERATOR: 'https://buy.paddle.com/operator', VITE_MPL_SOURCE_URL: 'https://hypertwist.app/open-source/source', VITE_OPEN_SOURCE_REPO_URL: 'https://git.scriptoriumai.io/scriptoriumadmin/hypertwist', }, serverEnv: { API_DOMAIN: 'https://hypertwist.app', WEBSITE_DOMAIN: 'https://hypertwist.app', SUPERTOKENS_CORE_URI: 'https://auth-core.internal', COOKIE_SECURE: 'true', PADDLE_WEBHOOK_SECRET: 'secret', PADDLE_PRICE_PLAN_MAP: '{"pri_operator":"operator"}', }, liveHealth: null, }) expect(report.warnings).toContain('SERVE_STATIC_WEBSITE is not explicitly set; confirm ../dist is present for first-party same-origin serving or that an external same-origin web server serves the frontend.') }) it('fails localhost-grade posture and missing public-launch configuration', () => { const report = buildRuntimeReadinessReport({ frontendEnv: { VITE_SUPERTOKENS_API_DOMAIN: 'http://localhost:3001', VITE_SUPERTOKENS_WEBSITE_DOMAIN: 'http://localhost:4273', VITE_AUTH_API_BASE_URL: 'http://localhost:3001', }, serverEnv: { API_DOMAIN: 'http://localhost:3001', WEBSITE_DOMAIN: 'http://localhost:4273', SUPERTOKENS_CORE_URI: 'http://localhost:3567', COOKIE_SECURE: 'false', }, liveHealth: null, }) expect(report.ok).toBe(false) expect(report.failures).toContain('VITE_SUPERTOKENS_API_DOMAIN still targets a loopback/local-development origin.') expect(report.failures).toContain('COOKIE_SECURE must be true before public launch.') expect(report.failures).toContain('PADDLE_WEBHOOK_SECRET is missing.') }) it('fails placeholder values even when production-shaped env files are otherwise populated', () => { const report = buildRuntimeReadinessReport({ frontendEnv: { VITE_SUPERTOKENS_API_DOMAIN: 'https://hypertwist.app', VITE_SUPERTOKENS_WEBSITE_DOMAIN: 'https://hypertwist.app', VITE_AUTH_API_BASE_URL: 'https://hypertwist.app', VITE_WINDOWS_DOWNLOAD_URL: 'https://downloads.hypertwist.app/replace-me/windows.exe', VITE_PADDLE_CHECKOUT_URL_OPERATOR: 'https://buy.paddle.com/replace-me-operator', VITE_PADDLE_CHECKOUT_URL_STUDIO: 'https://buy.paddle.com/replace-me-studio', VITE_MPL_SOURCE_URL: 'https://hypertwist.app/open-source/replace-me', VITE_OPEN_SOURCE_REPO_URL: 'https://git.scriptoriumai.io/scriptoriumadmin/hypertwist', }, serverEnv: { API_DOMAIN: 'https://hypertwist.app', WEBSITE_DOMAIN: 'https://hypertwist.app', SUPERTOKENS_CORE_URI: 'http://127.0.0.1:3567', COOKIE_SECURE: 'true', PADDLE_WEBHOOK_SECRET: 'replace-me-paddle-webhook-secret', PADDLE_PRICE_PLAN_MAP: '{"replace_me_price_operator":"operator"}', }, liveHealth: null, }) expect(report.ok).toBe(false) expect(report.failures).toContain('VITE_WINDOWS_DOWNLOAD_URL still contains a placeholder value.') expect(report.failures).toContain('VITE_PADDLE_CHECKOUT_URL_OPERATOR still contains a placeholder value.') expect(report.failures).toContain('PADDLE_WEBHOOK_SECRET still contains a placeholder value.') expect(report.failures).toContain('PADDLE_PRICE_PLAN_MAP still contains a placeholder value.') }) it('accepts server-backed release-manifest download and source posture when frontend fallback URLs are absent', () => { const report = buildRuntimeReadinessReport({ frontendEnv: { VITE_SUPERTOKENS_API_DOMAIN: 'https://hypertwist.app', VITE_SUPERTOKENS_WEBSITE_DOMAIN: 'https://hypertwist.app', VITE_AUTH_API_BASE_URL: 'https://hypertwist.app', VITE_PADDLE_CHECKOUT_URL_OPERATOR: 'https://buy.paddle.com/operator', VITE_PADDLE_CHECKOUT_URL_STUDIO: 'https://buy.paddle.com/studio', }, serverEnv: { API_DOMAIN: 'https://hypertwist.app', WEBSITE_DOMAIN: 'https://hypertwist.app', SUPERTOKENS_CORE_URI: 'https://auth-core.internal', COOKIE_SECURE: 'true', PADDLE_WEBHOOK_SECRET: 'secret', PADDLE_PRICE_PLAN_MAP: '{"pri_operator":"operator"}', WINDOWS_DOWNLOAD_URL: 'https://downloads.hypertwist.app/windows.exe', MPL_SOURCE_URL: 'https://hypertwist.app/open-source/source', OPEN_SOURCE_REPO_URL: 'https://git.scriptoriumai.io/scriptoriumadmin/hypertwist', }, liveHealth: { supertokens: { ready: true }, fallback: { active: false }, runtime: { public_origin_ready: true, mode: 'public', errors: [], warnings: [], }, billing: { webhookSecretConfigured: true, productPlanMapConfigured: false, pricePlanMapConfigured: true, }, }, }) expect(report.ok).toBe(true) expect(report.failures).toEqual([]) }) it('fails live deployment checks when the public manifest leaks URLs or the root page is still placeholder-backed', () => { const report = buildRuntimeReadinessReport({ frontendEnv: { VITE_SUPERTOKENS_API_DOMAIN: 'https://hypertwist.app', VITE_SUPERTOKENS_WEBSITE_DOMAIN: 'https://hypertwist.app', VITE_AUTH_API_BASE_URL: 'https://hypertwist.app', VITE_PADDLE_CHECKOUT_URL_OPERATOR: 'https://buy.paddle.com/operator', VITE_MPL_SOURCE_URL: 'https://hypertwist.app/open-source/source', VITE_OPEN_SOURCE_REPO_URL: 'https://git.scriptoriumai.io/scriptoriumadmin/hypertwist', VITE_WINDOWS_DOWNLOAD_URL: 'https://downloads.hypertwist.app/windows.exe', }, serverEnv: { API_DOMAIN: 'https://hypertwist.app', WEBSITE_DOMAIN: 'https://hypertwist.app', SUPERTOKENS_CORE_URI: 'https://auth-core.internal', COOKIE_SECURE: 'true', PADDLE_WEBHOOK_SECRET: 'secret', PADDLE_PRICE_PLAN_MAP: '{"pri_operator":"operator"}', }, liveHealth: { supertokens: { ready: true }, fallback: { active: false }, runtime: { public_origin_ready: true, mode: 'public', errors: [], warnings: [], }, billing: { webhookSecretConfigured: true, productPlanMapConfigured: false, pricePlanMapConfigured: true, }, }, liveReleaseManifest: { ok: true, manifest: { viewer: { authenticated: false, canDownload: false, }, platforms: [ { platform_key: 'windows', configured: true, download_url: 'https://downloads.hypertwist.app/windows.exe', }, ], }, }, liveWebsiteShell: { title: 'HyperTwist', hasShellMarker: false, hasRootMount: false, placeholderDetected: true, }, liveHealthAttempted: true, liveReleaseManifestAttempted: true, liveWebsiteShellAttempted: true, }) expect(report.ok).toBe(false) expect(report.failures).toContain('Live public release manifest exposes a raw download URL to anonymous viewers.') expect(report.failures).toContain('Live website root still serves the placeholder rollout page instead of the first-party HyperTwist shell.') expect(report.failures).toContain('Live website root is missing the first-party HyperTwist shell marker.') expect(report.failures).toContain('Live website root is missing the expected #root app mount.') }) }) describe('deriveHealthBaseUrl', () => { it('prefers the explicit health URL over env-derived defaults', () => { const baseUrl = deriveHealthBaseUrl({ explicitHealthUrl: 'https://hypertwist.app/', frontendEnv: { VITE_AUTH_API_BASE_URL: 'https://auth.hypertwist.app', }, serverEnv: { API_DOMAIN: 'https://server.hypertwist.app', }, }) expect(baseUrl).toBe('https://hypertwist.app') }) })