"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const index_js_1 = require("../../types/index.js");
const postgres_js_1 = require("./postgres.js");
const datasourceItems = postgres_js_1.PostgresPlugin.datasourceTemplate?.sections.flatMap((section) => section.items.flatMap(index_js_1.getRowItemsFromSectionItem));
const findDatasourceItems = (name) => datasourceItems?.filter((item) => item.name === name) ?? [];
(0, vitest_1.describe)('Postgres datasource template', () => {
    (0, vitest_1.it)('gates IAM fields only by Postgres plugin version 0.0.13', () => {
        const iamFieldNames = [
            'authentication.authType',
            'authentication.custom.databaseName.value',
            'authentication.custom.iamRoleArn.value',
            'authentication.custom.region.value',
            'authentication.custom.sessionPolicy.value',
            'authentication.username',
            'connection.useSsl'
        ];
        for (const fieldName of iamFieldNames) {
            const versionThirteenItems = findDatasourceItems(fieldName).filter((item) => item.startVersion === '0.0.13');
            (0, vitest_1.expect)(versionThirteenItems.length).toBeGreaterThan(0);
            (0, vitest_1.expect)(versionThirteenItems.every((item) => item.ldFlag === undefined)).toBe(true);
        }
    });
    (0, vitest_1.it)('adds the version 0.0.13 authentication method and IAM role fields', () => {
        (0, vitest_1.expect)(findDatasourceItems('authentication.authType')).toEqual([
            vitest_1.expect.objectContaining({
                componentType: index_js_1.FormComponentType.DROPDOWN,
                display: {
                    show: {
                        connectionType: ['fields']
                    }
                },
                initialValue: 'password',
                options: [
                    {
                        displayName: 'Username & password',
                        key: 'password',
                        value: 'password'
                    },
                    {
                        displayName: 'AWS IAM Role',
                        key: 'aws_iam_role',
                        value: 'aws_iam_role'
                    }
                ],
                startVersion: '0.0.13'
            })
        ]);
        (0, vitest_1.expect)(findDatasourceItems('authentication.custom.iamRoleArn.value')).toEqual([
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        'authentication.authType': ['aws_iam_role'],
                        connectionType: ['fields']
                    }
                },
                rules: [{ message: 'Role ARN is required', required: true }],
                startVersion: '0.0.13'
            })
        ]);
        (0, vitest_1.expect)(findDatasourceItems('authentication.custom.region.value')).toEqual([
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        'authentication.authType': ['aws_iam_role'],
                        connectionType: ['fields']
                    }
                },
                rules: [{ message: 'Region is required', required: true }],
                startVersion: '0.0.13'
            })
        ]);
        (0, vitest_1.expect)(findDatasourceItems('authentication.custom.sessionPolicy.value')).toEqual([
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        'authentication.authType': ['aws_iam_role'],
                        connectionType: ['fields']
                    }
                },
                rules: [{ required: false }],
                startVersion: '0.0.13',
                style: {
                    height: '100px'
                }
            })
        ]);
    });
    (0, vitest_1.it)('places session policy after the required IAM username field', () => {
        const iamFieldOrder = datasourceItems
            ?.filter((item) => item.startVersion === '0.0.13')
            .filter((item) => item.display?.show?.['authentication.authType']?.includes('aws_iam_role'))
            .map((item) => item.name) ?? [];
        const usernameIndex = iamFieldOrder.indexOf('authentication.username');
        const sessionPolicyIndex = iamFieldOrder.indexOf('authentication.custom.sessionPolicy.value');
        (0, vitest_1.expect)(usernameIndex).toBeGreaterThanOrEqual(0);
        (0, vitest_1.expect)(sessionPolicyIndex).toBeGreaterThan(usernameIndex);
    });
    (0, vitest_1.it)('keeps username visible and password compatible with legacy configurations', () => {
        (0, vitest_1.expect)(findDatasourceItems('authentication.username')).toEqual([
            vitest_1.expect.objectContaining({
                endVersion: '0.0.10',
                startVersion: '0.0.1'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        connectionType: ['fields']
                    }
                },
                endVersion: '0.0.12',
                startVersion: '0.0.11'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        'authentication.authType': ['', 'undefined', 'password'],
                        connectionType: ['fields']
                    }
                },
                startVersion: '0.0.13'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        'authentication.authType': ['aws_iam_role'],
                        connectionType: ['fields']
                    }
                },
                rules: [{ message: 'Database username is required', required: true }],
                startVersion: '0.0.13'
            })
        ]);
        (0, vitest_1.expect)(findDatasourceItems('authentication.password')).toEqual([
            vitest_1.expect.objectContaining({
                endVersion: '0.0.10',
                startVersion: '0.0.1'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        connectionType: ['fields']
                    }
                },
                endVersion: '0.0.12',
                startVersion: '0.0.11'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        'authentication.authType': ['', 'undefined', 'password'],
                        connectionType: ['fields']
                    }
                },
                startVersion: '0.0.13'
            })
        ]);
    });
    (0, vitest_1.it)('requires a database name only for IAM role authentication', () => {
        (0, vitest_1.expect)(findDatasourceItems('authentication.custom.databaseName.value')).toEqual([
            vitest_1.expect.objectContaining({
                endVersion: '0.0.10',
                startVersion: '0.0.1'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        connectionType: ['fields']
                    }
                },
                endVersion: '0.0.12',
                startVersion: '0.0.11'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        'authentication.authType': ['', 'undefined', 'password'],
                        connectionType: ['fields']
                    }
                },
                startVersion: '0.0.13'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        'authentication.authType': ['aws_iam_role'],
                        connectionType: ['fields']
                    }
                },
                rules: [{ message: 'Database name is required', required: true }],
                startVersion: '0.0.13'
            })
        ]);
    });
    (0, vitest_1.it)('requires TLS for IAM role authentication without changing password mode', () => {
        (0, vitest_1.expect)(findDatasourceItems('connection.useSsl')).toEqual([
            vitest_1.expect.objectContaining({
                endVersion: '0.0.10',
                startVersion: '0.0.1'
            }),
            vitest_1.expect.objectContaining({
                display: {
                    show: {
                        connectionType: ['fields']
                    }
                },
                endVersion: '0.0.12',
                startVersion: '0.0.11'
            }),
            vitest_1.expect.objectContaining({
                disabled: false,
                display: {
                    show: {
                        'authentication.authType': ['', 'undefined', 'password'],
                        connectionType: ['fields']
                    }
                },
                initialValue: true,
                startVersion: '0.0.13'
            }),
            vitest_1.expect.objectContaining({
                disabled: true,
                display: {
                    show: {
                        'authentication.authType': ['aws_iam_role'],
                        connectionType: ['fields']
                    }
                },
                initialValue: true,
                rules: [{ enum: [true], message: 'SSL is required for AWS IAM role authentication' }],
                startVersion: '0.0.13'
            })
        ]);
    });
});
//# sourceMappingURL=postgres.test.js.map