import { describe, it, expect, vi } from 'vitest'; import { normalizeApplicationUrl, matchesApplicationAccess } from '@wishforge/contracts'; import { ComposeDeploymentAdapter, KubernetesDeploymentAdapter, type ReleaseManifest } from '../src/index.js'; import { verifyHttp, validateRelease, deliveryConfiguration } from '../src/deploy.js'; const release: ReleaseManifest = { schemaVersion: 1, id: 'release', tenantId: 'tenant', environmentId: 'env', sourceCommit: 'a'.repeat(40), image: `registry.example/web@sha256:${'b'.repeat(64)}`, containerPort: 3000, healthPath: '/health', smokeChecks: [{ path: '/catalog', expectedStatus: 200, bodyIncludes: 'record' }], expectedPreviousReleaseId: null, applicationUrl: 'https://app.example.test/catalog', deliveryRequired: true, }; describe('declared application URL and actual stable access acceptance', () => { it('rejects inline credentials, query, fragment, insecure and internal service addresses', () => { for (const value of [ 'http://app.example.test', 'https://u:p@app.example.test', 'https://app.example.test/?token=x', 'https://app.example.test/#x', 'https://app.example.test/?', 'https://10.0.0.2', 'https://172.31.1.2', 'https://service.ns.svc.cluster.local', 'https://service', 'https://[::1]', 'https://app.example.test/\\evil', ]) expect(() => normalizeApplicationUrl(value)).toThrow(); expect(normalizeApplicationUrl('https://app.example.test')).toBe('https://app.example.test/'); expect(() => normalizeApplicationUrl('http://127.0.0.1:9999')).toThrow(); expect(normalizeApplicationUrl('http://127.0.0.1:9999', true)).toBe('http://127.0.0.1:9999/'); }); it('requires URL and access configuration before deployment, with no command or network side effects', async () => { const execute = vi.fn(), fetcher = vi.fn(); for (const adapter of [ new ComposeDeploymentAdapter({ healthUrl: 'https://app.example.test' }, execute, fetcher), new KubernetesDeploymentAdapter({ healthUrl: 'https://app.example.test' }, execute, fetcher), ]) { expect((await adapter.deploy({ ...release, applicationUrl: undefined }, { execute: true })).status).toBe( 'unconfigured', ); expect((await adapter.reconcile({ ...release, applicationUrl: 'https://other.example.test/' })).status).toBe( 'unconfigured', ); } expect(execute).not.toHaveBeenCalled(); expect(fetcher).not.toHaveBeenCalled(); }); it('records the exact external URL only after stable health, business response and anonymous application probe pass', async () => { const calls: string[] = []; const fetcher: typeof fetch = async url => { calls.push(String(url)); return new Response('record'); }; const result = await verifyHttp(release, 'https://app.example.test/', fetcher, { stable: true }); expect(result).toMatchObject({ passed: true, deliveryUrl: release.applicationUrl, accessEvidence: { applicationUrl: release.applicationUrl, stableOrigin: 'https://app.example.test', checks: ['application-url', 'public-access'], }, }); expect(calls).toEqual([ 'https://app.example.test/health', 'https://app.example.test/catalog', 'https://app.example.test/catalog', ]); }); it('rejects changed URL, wrong stable origin and stale or future access evidence', () => { const evidence = { applicationUrl: release.applicationUrl, stableOrigin: 'https://app.example.test', checkedAt: new Date().toISOString(), checks: ['application-url', 'public-access'], }; expect(matchesApplicationAccess(release, evidence)).toBe(true); for (const change of [ { applicationUrl: 'https://other.example.test/' }, { stableOrigin: 'https://other.example.test' }, { checkedAt: new Date(Date.now() - 600000).toISOString() }, { checkedAt: new Date(Date.now() + 600000).toISOString() }, ]) expect(matchesApplicationAccess(release, { ...evidence, ...change })).toBe(false); }); it('candidate-only checks never create delivery evidence, and denied final application URL cannot be delivered', async () => { expect(await verifyHttp(release, 'http://candidate.ns.svc/', async () => new Response('record'))).toMatchObject({ passed: true, }); const candidate = await verifyHttp(release, 'http://candidate.ns.svc/', async () => new Response('record')); expect(candidate).not.toHaveProperty('deliveryUrl'); let count = 0; const final = await verifyHttp( release, 'https://app.example.test/', async () => (++count < 3 ? new Response('record') : new Response('', { status: 401 })), { stable: true }, ); expect(final.passed).toBe(false); expect(final).not.toHaveProperty('deliveryUrl'); }); it('does not accept stable origin drift as public access', async () => { const fetcher = vi.fn(); expect((await verifyHttp(release, 'https://other.example.test/', fetcher, { stable: true })).passed).toBe(false); expect(fetcher).not.toHaveBeenCalled(); expect( deliveryConfiguration({ ...release, applicationUrl: undefined, deliveryRequired: false }, undefined), ).toBeUndefined(); expect(() => validateRelease({ ...release, applicationUrl: 'https://app.example.test' })).toThrow(); }); });