"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const previewDeployWorkflow_1 = require("./previewDeployWorkflow");
describe('isWorkflowFile', () => {
    it('matches yml/yaml under .github/workflows', () => {
        expect((0, previewDeployWorkflow_1.isWorkflowFile)('.github/workflows/start-preview.yml')).toBe(true);
        expect((0, previewDeployWorkflow_1.isWorkflowFile)('.github/workflows/ci.yaml')).toBe(true);
        expect((0, previewDeployWorkflow_1.isWorkflowFile)('./.github/workflows/ci.yml')).toBe(true);
    });
    it('rejects non-workflow paths', () => {
        expect((0, previewDeployWorkflow_1.isWorkflowFile)('.github/dependabot.yml')).toBe(false);
        expect((0, previewDeployWorkflow_1.isWorkflowFile)('models/orders.sql')).toBe(false);
        expect((0, previewDeployWorkflow_1.isWorkflowFile)('.github/workflows/README.md')).toBe(false);
    });
});
describe('detectPreviewDeployWorkflow', () => {
    it('detects a start-preview workflow', () => {
        const files = [
            {
                path: '.github/workflows/start-preview.yml',
                content: 'run: lightdash start-preview --name "$BRANCH"',
            },
        ];
        expect((0, previewDeployWorkflow_1.detectPreviewDeployWorkflow)(files)).toEqual({
            hasPreviewDeployWorkflow: true,
            workflowPath: '.github/workflows/start-preview.yml',
        });
    });
    it('does not treat a production deploy workflow as a preview deploy', () => {
        const files = [
            {
                path: '.github/workflows/deploy.yml',
                content: 'run: lightdash deploy --project-dir .',
            },
        ];
        expect((0, previewDeployWorkflow_1.detectPreviewDeployWorkflow)(files)).toEqual({
            hasPreviewDeployWorkflow: false,
            workflowPath: null,
        });
    });
    it('ignores matching markers outside the workflows dir', () => {
        const files = [
            {
                path: 'docs/lightdash start-preview.md',
                content: 'lightdash start-preview',
            },
        ];
        expect((0, previewDeployWorkflow_1.detectPreviewDeployWorkflow)(files).hasPreviewDeployWorkflow).toBe(false);
    });
    it('returns false for an empty repo', () => {
        expect((0, previewDeployWorkflow_1.detectPreviewDeployWorkflow)([])).toEqual({
            hasPreviewDeployWorkflow: false,
            workflowPath: null,
        });
    });
});
describe('getPreviewDeploySecrets', () => {
    const secrets = (0, previewDeployWorkflow_1.getPreviewDeploySecrets)({
        projectUuid: 'proj-123',
        siteUrl: 'https://app.lightdash.cloud',
    });
    it('pre-fills the project UUID and instance URL', () => {
        const byName = Object.fromEntries(secrets.map((s) => [s.name, s.value]));
        expect(byName.LIGHTDASH_PROJECT).toBe('proj-123');
        expect(byName.LIGHTDASH_URL).toBe('https://app.lightdash.cloud');
    });
    it('leaves user-supplied secrets null', () => {
        const byName = Object.fromEntries(secrets.map((s) => [s.name, s.value]));
        expect(byName.LIGHTDASH_API_KEY).toBeNull();
        expect(byName.DBT_PROFILES).toBeNull();
    });
    it('links to the personal access tokens page (no double slash)', () => {
        const apiKey = secrets.find((s) => s.name === 'LIGHTDASH_API_KEY');
        expect(apiKey?.description).toContain('https://app.lightdash.cloud/generalSettings/personalAccessTokens');
        const trailing = (0, previewDeployWorkflow_1.getPreviewDeploySecrets)({
            projectUuid: 'p',
            siteUrl: 'https://app.lightdash.cloud/',
        }).find((s) => s.name === 'LIGHTDASH_API_KEY');
        expect(trailing?.description).toContain('https://app.lightdash.cloud/generalSettings/personalAccessTokens');
        expect(trailing?.description).not.toContain('cloud//general');
    });
});
describe('generatePreviewDeployWorkflowFiles', () => {
    it('generates a start- and close-preview pair under .github/workflows', () => {
        const files = (0, previewDeployWorkflow_1.generatePreviewDeployWorkflowFiles)({
            projectSubPath: 'dbt',
            cliVersion: '0.3075.2',
        });
        const paths = files.map((f) => f.path).sort();
        expect(paths).toEqual([
            '.github/workflows/close-preview.yml',
            '.github/workflows/start-preview.yml',
        ]);
    });
    it('wires the project subpath into --project-dir and is detectable by its own detector', () => {
        const files = (0, previewDeployWorkflow_1.generatePreviewDeployWorkflowFiles)({
            projectSubPath: 'analytics/dbt',
            cliVersion: '0.3075.2',
        });
        const start = files.find((f) => f.path.endsWith('start-preview.yml'));
        expect(start?.content).toContain('PROJECT_DIR: analytics/dbt');
        expect(start?.content).toContain('lightdash start-preview');
        // The thing we generate must be recognised as "set up" on the next scan.
        expect((0, previewDeployWorkflow_1.detectPreviewDeployWorkflow)(files).hasPreviewDeployWorkflow).toBe(true);
    });
    it('defaults the project dir to the repo root when subpath is null', () => {
        const files = (0, previewDeployWorkflow_1.generatePreviewDeployWorkflowFiles)({
            projectSubPath: null,
            cliVersion: '0.3075.2',
        });
        const start = files.find((f) => f.path.endsWith('start-preview.yml'));
        expect(start?.content).toContain('PROJECT_DIR: .');
    });
    it('pins @lightdash/cli to the supplied (instance) version in both files', () => {
        const files = (0, previewDeployWorkflow_1.generatePreviewDeployWorkflowFiles)({
            projectSubPath: 'dbt',
            cliVersion: '1.2.3',
        });
        files.forEach(({ content }) => {
            expect(content).toContain('npm install -g @lightdash/cli@1.2.3');
        });
    });
    it('hardens both workflows: SHA-pinned actions, least-privilege permissions, pinned CLI, timeout', () => {
        const files = (0, previewDeployWorkflow_1.generatePreviewDeployWorkflowFiles)({
            projectSubPath: 'dbt',
            cliVersion: '0.3075.2',
        });
        files.forEach(({ content }) => {
            // Actions pinned to a 40-char commit SHA, never a floating tag.
            expect(content).toMatch(/actions\/checkout@[0-9a-f]{40}/);
            expect(content).toMatch(/actions\/setup-node@[0-9a-f]{40}/);
            expect(content).not.toMatch(/uses: actions\/\S+@v\d/);
            // Least-privilege token: read-only, and never pull-requests:write
            // (it would zero `contents` and break checkout on private repos).
            expect(content).toContain('permissions:\n  contents: read');
            expect(content).not.toContain('pull-requests: write');
            // CLI pinned for reproducibility; job bounded by a timeout.
            expect(content).toMatch(/@lightdash\/cli@\d+\.\d+\.\d+/);
            expect(content).toContain('timeout-minutes:');
        });
    });
});
//# sourceMappingURL=previewDeployWorkflow.test.js.map