"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const index_js_1 = require("./index.js");
(0, vitest_1.describe)('database lifecycle contracts', () => {
    (0, vitest_1.test)('computes stable requirement, binding, and desired spec identifiers', async () => {
        const requirement = {
            logicalName: 'Orders DB',
            engine: 'postgres',
            version: '16',
            sizing: {
                storageGb: 20,
                tier: 'shared'
            },
            extensions: ['pgcrypto', 'uuid-ossp']
        };
        (0, vitest_1.expect)((0, index_js_1.computeRequirementKey)(requirement)).toBe('orders-db~Orders%20DB:postgres');
        (0, vitest_1.expect)((0, index_js_1.computeRequirementKey)({ logicalName: 'orders/db', engine: 'postgres' })).toBe('orders-db~orders%2Fdb:postgres');
        (0, vitest_1.expect)((0, index_js_1.computeRequirementKey)({ logicalName: 'orders-db', engine: 'postgres' })).toBe('orders-db~orders-db:postgres');
        (0, vitest_1.expect)((0, index_js_1.computeBindingKey)({
            organizationId: 'org-1',
            applicationId: 'app-1',
            environment: 'edit',
            profile: 'Staging EU',
            requirementKey: 'orders-db~Orders%20DB:postgres'
        })).toBe('org-1:app-1:edit:staging-eu~Staging%20EU:orders-db~Orders%20DB:postgres');
        (0, vitest_1.expect)((0, index_js_1.computeBindingKey)({
            organizationId: 'org-1',
            applicationId: 'app-1',
            environment: 'edit',
            profile: 'staging',
            requirementKey: 'orders-db~Orders%20DB:postgres'
        })).toBe('org-1:app-1:edit:staging~staging:orders-db~Orders%20DB:postgres');
        await (0, vitest_1.expect)((0, index_js_1.computeDesiredSpecHash)(requirement)).resolves.toBe(await (0, index_js_1.computeDesiredSpecHash)({ ...requirement, extensions: ['uuid-ossp', 'pgcrypto'] }));
        await (0, vitest_1.expect)((0, index_js_1.computeDesiredSpecHash)(requirement)).resolves.toBe('QsCl6RVzJVEwf4Nuk0D27rRtycYe2vQZMxcXLt9WUkQ=');
    });
    (0, vitest_1.test)('hashes desired specs with locale-invariant ordering', async () => {
        const first = await (0, index_js_1.computeDesiredSpecHash)({
            logicalName: 'Éclair DB',
            engine: 'postgres',
            extensions: ['ä', 'z', 'a']
        });
        const second = await (0, index_js_1.computeDesiredSpecHash)({
            logicalName: 'Éclair DB',
            engine: 'postgres',
            extensions: ['z', 'a', 'ä']
        });
        (0, vitest_1.expect)(first).toBe(second);
    });
    (0, vitest_1.test)('redacts raw credential material while preserving credential refs', () => {
        const redacted = (0, index_js_1.redactLifecycleSecrets)({
            host: 'db.internal',
            password: 'raw-password',
            nested: {
                token: 'raw-token',
                credentialRef: {
                    resolver: 'aws_secrets_manager',
                    ref: 'arn:aws:secretsmanager:us-east-1:123:secret:orders'
                }
            },
            credential_refs: {
                password: {
                    resolver: 'vault',
                    ref: 'database/orders',
                    field: 'password'
                }
            }
        });
        (0, vitest_1.expect)(redacted).toEqual({
            host: 'db.internal',
            password: '[REDACTED]',
            nested: {
                token: '[REDACTED]',
                credentialRef: {
                    resolver: 'aws_secrets_manager',
                    ref: 'arn:aws:secretsmanager:us-east-1:123:secret:orders'
                }
            },
            credential_refs: {
                password: {
                    resolver: 'vault',
                    ref: 'database/orders',
                    field: 'password'
                }
            }
        });
    });
    (0, vitest_1.test)('recognizes terminal lifecycle states', () => {
        (0, vitest_1.expect)((0, index_js_1.isTerminalLifecycleState)('ready')).toBe(true);
        (0, vitest_1.expect)((0, index_js_1.isTerminalLifecycleState)('failed')).toBe(true);
        (0, vitest_1.expect)((0, index_js_1.isTerminalLifecycleState)('provisioning')).toBe(false);
    });
    (0, vitest_1.test)('flags secret-shaped keys', () => {
        for (const key of ['password', 'PASSWORD', 'api_token', 'private_key', 'private-key', 'dsn']) {
            (0, vitest_1.expect)((0, index_js_1.isSecretKey)(key)).toBe(true);
        }
        for (const key of ['host', 'port', 'database', 'username']) {
            (0, vitest_1.expect)((0, index_js_1.isSecretKey)(key)).toBe(false);
        }
    });
    (0, vitest_1.test)('flags secret-shaped value content', () => {
        (0, vitest_1.expect)((0, index_js_1.containsCredentialMaterial)('postgres://user:pass@db.internal:5432/orders')).toBe(true);
        (0, vitest_1.expect)((0, index_js_1.containsCredentialMaterial)('mongodb+srv://app:secret@cluster0.example.net/db')).toBe(true);
        (0, vitest_1.expect)((0, index_js_1.containsCredentialMaterial)('password=hunter2;host=db.internal')).toBe(true);
        (0, vitest_1.expect)((0, index_js_1.containsCredentialMaterial)('{"password":"hunter2"}')).toBe(true);
        (0, vitest_1.expect)((0, index_js_1.containsCredentialMaterial)('host=db.internal port=5432')).toBe(false);
        (0, vitest_1.expect)((0, index_js_1.containsCredentialMaterial)('arn:aws:secretsmanager:us-east-1:123:secret:orders')).toBe(false);
    });
});
//# sourceMappingURL=index.test.js.map