"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const strict_1 = __importDefault(require("node:assert/strict"));
const node_fs_1 = __importDefault(require("node:fs"));
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
const node_test_1 = __importDefault(require("node:test"));
const deployment_1 = require("../src/deployment");
(0, node_test_1.default)('deploy role parsing stays strict', () => {
    strict_1.default.equal((0, deployment_1.parseDeployRole)('prod'), 'prod');
    strict_1.default.equal((0, deployment_1.parseDeployRole)('preview'), 'preview');
    strict_1.default.equal((0, deployment_1.parseDeployRole)('blue'), null);
    strict_1.default.equal((0, deployment_1.parseDeployRole)(undefined), null);
});
(0, node_test_1.default)('deploy slot parsing stays strict', () => {
    strict_1.default.equal((0, deployment_1.parseDeploySlot)('blue'), 'blue');
    strict_1.default.equal((0, deployment_1.parseDeploySlot)('green'), 'green');
    strict_1.default.equal((0, deployment_1.parseDeploySlot)('prod'), null);
});
(0, node_test_1.default)('prod defaults resolve to public production origins', () => {
    const env = { NODE_ENV: 'production' };
    strict_1.default.equal((0, deployment_1.resolveDeployRole)(env), 'prod');
    strict_1.default.equal((0, deployment_1.resolvePublicSurface)(env), 'prod');
    strict_1.default.equal((0, deployment_1.resolveWebOrigin)(env), 'https://www.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveApiOrigin)(env), 'https://api.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveAiOrigin)(env), 'https://ai.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveRealtimeOrigin)(env), 'wss://play.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveSeoIndexingMode)(env), 'public');
    strict_1.default.equal((0, deployment_1.isBackgroundWorkEnabled)(env), true);
});
(0, node_test_1.default)('preview defaults resolve to preview origins and noindex seo', () => {
    const env = {
        NODE_ENV: 'production',
        PLAYDROP_DEPLOY_ROLE: 'preview',
        PLAYDROP_SLOT: 'green',
    };
    strict_1.default.equal((0, deployment_1.resolveDeployRole)(env), 'preview');
    strict_1.default.equal((0, deployment_1.resolvePublicSurface)(env), 'preview');
    strict_1.default.equal((0, deployment_1.resolveWebOrigin)(env), 'https://preview.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveApiOrigin)(env), 'https://api.preview.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveAiOrigin)(env), 'https://ai.preview.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveRealtimeOrigin)(env), 'wss://play.preview.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveSeoIndexingMode)(env), 'noindex');
    strict_1.default.equal((0, deployment_1.isBackgroundWorkEnabled)(env), false);
});
(0, node_test_1.default)('background work follows the runtime slot-state file when present', () => {
    const tempDir = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), 'playdrop-slot-state-'));
    const slotStatePath = node_path_1.default.join(tempDir, 'slot-state.json');
    try {
        node_fs_1.default.writeFileSync(slotStatePath, JSON.stringify({ activeSlot: 'green' }));
        const env = {
            NODE_ENV: 'production',
            PLAYDROP_DEPLOY_ROLE: 'prod',
            PLAYDROP_SLOT: 'green',
            PLAYDROP_SLOT_STATE_PATH: slotStatePath,
        };
        strict_1.default.equal((0, deployment_1.isCurrentSlotActive)(env), true);
        strict_1.default.equal((0, deployment_1.isBackgroundWorkEnabled)(env), true);
        node_fs_1.default.writeFileSync(slotStatePath, JSON.stringify({ activeSlot: 'blue' }));
        strict_1.default.equal((0, deployment_1.isCurrentSlotActive)(env), false);
        strict_1.default.equal((0, deployment_1.isBackgroundWorkEnabled)(env), false);
    }
    finally {
        node_fs_1.default.rmSync(tempDir, { recursive: true, force: true });
    }
});
(0, node_test_1.default)('explicit origin overrides win over derived defaults', () => {
    const env = {
        PLAYDROP_PUBLIC_ORIGIN: 'https://blue.playdrop.ai/',
        PLAYDROP_API_BASE_URL: 'https://api.blue.playdrop.ai/',
        PLAYDROP_AI_BASE_URL: 'https://ai.blue.playdrop.ai/',
        PLAYDROP_REALTIME_BASE_URL: 'wss://play.blue.playdrop.ai/ws/',
        PLAYDROP_PUBLIC_SURFACE: 'blue',
    };
    strict_1.default.equal((0, deployment_1.resolvePublicSurface)(env), 'blue');
    strict_1.default.equal((0, deployment_1.resolveWebOrigin)(env), 'https://blue.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveApiOrigin)(env), 'https://api.blue.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveAiOrigin)(env), 'https://ai.blue.playdrop.ai');
    strict_1.default.equal((0, deployment_1.resolveRealtimeOrigin)(env), 'wss://play.blue.playdrop.ai/ws');
});
(0, node_test_1.default)('structured log fields include phase 1 deploy metadata', () => {
    const fields = (0, deployment_1.resolveStructuredLogFields)('api-server', {
        NODE_ENV: 'production',
        PLAYDROP_DEPLOY_ROLE: 'preview',
        PLAYDROP_SLOT: 'green',
        PLAYDROP_PUBLIC_SURFACE: 'preview',
    });
    strict_1.default.equal(fields.environment, 'production');
    strict_1.default.equal(fields.slot, 'green');
    strict_1.default.equal(fields.service, 'api-server');
    strict_1.default.equal(fields.deploy_role, 'preview');
    strict_1.default.equal(fields.public_surface, 'preview');
    strict_1.default.match(fields.release_version, /^\d+\.\d+\.\d+$/);
    strict_1.default.equal(typeof fields.build_number, 'number');
});
