"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const extensionsEmulator_1 = require("../../emulator/extensionsEmulator");
const types_1 = require("../../extensions/types");
const TEST_EXTENSION = {
    name: "publishers/firebase/extensions/storage-resize-images",
    ref: "firebase/storage-resize-images",
    visibility: types_1.Visibility.PUBLIC,
    registryLaunchStage: types_1.RegistryLaunchStage.BETA,
    createTime: "0",
};
const TEST_EXTENSION_VERSION = {
    name: "publishers/firebase/extensions/storage-resize-images/versions/0.1.18",
    ref: "firebase/storage-resize-images@0.1.18",
    state: "PUBLISHED",
    sourceDownloadUri: "https://fake.test",
    hash: "abc123",
    spec: {
        name: "publishers/firebase/extensions/storage-resize-images/versions/0.1.18",
        resources: [
            {
                type: "firebaseextensions.v1beta.function",
                name: "generateResizedImage",
                description: `Listens for new images uploaded to your specified Cloud Storage bucket, resizes the images,
      then stores the resized images in the same bucket. Optionally keeps or deletes the original images.`,
                properties: {
                    location: "${param:LOCATION}",
                    runtime: "nodejs10",
                    eventTrigger: {
                        eventType: "google.storage.object.finalize",
                        resource: "projects/_/buckets/${param:IMG_BUCKET}",
                    },
                },
            },
        ],
        params: [],
        version: "0.1.18",
        sourceUrl: "https://fake.test",
    },
};
describe("Extensions Emulator", () => {
    describe("toEmulatableBackends", () => {
        let previousCachePath;
        beforeEach(() => {
            previousCachePath = process.env.FIREBASE_EXTENSIONS_CACHE_PATH;
            process.env.FIREBASE_EXTENSIONS_CACHE_PATH = "./src/test/emulators/extensions";
        });
        afterEach(() => {
            process.env.FIREBASE_EXTENSIONS_CACHE_PATH = previousCachePath;
        });
        const testCases = [
            {
                desc: "should transform a instance spec to a backend",
                input: {
                    instanceId: "ext-test",
                    ref: {
                        publisherId: "firebase",
                        extensionId: "storage-resize-images",
                        version: "0.1.18",
                    },
                    params: {
                        LOCATION: "us-west1",
                        ALLOWED_EVENT_TYPES: "google.firebase.image-resize-started,google.firebase.image-resize-completed",
                        EVENTARC_CHANNEL: "projects/test-project/locations/us-central1/channels/firebase",
                    },
                    allowedEventTypes: [
                        "google.firebase.image-resize-started",
                        "google.firebase.image-resize-completed",
                    ],
                    eventarcChannel: "projects/test-project/locations/us-central1/channels/firebase",
                    extension: TEST_EXTENSION,
                    extensionVersion: TEST_EXTENSION_VERSION,
                },
                expected: {
                    env: {
                        LOCATION: "us-west1",
                        DATABASE_INSTANCE: "test-project",
                        DATABASE_URL: "https://test-project.firebaseio.com",
                        EXT_INSTANCE_ID: "ext-test",
                        PROJECT_ID: "test-project",
                        STORAGE_BUCKET: "test-project.appspot.com",
                        ALLOWED_EVENT_TYPES: "google.firebase.image-resize-started,google.firebase.image-resize-completed",
                        EVENTARC_CHANNEL: "projects/test-project/locations/us-central1/channels/firebase",
                        EVENTARC_CLOUD_EVENT_SOURCE: "projects/test-project/instances/ext-test",
                    },
                    secretEnv: [],
                    extensionInstanceId: "ext-test",
                    functionsDir: (0, node_path_1.join)("src/test/emulators/extensions/firebase/storage-resize-images@0.1.18/functions"),
                    nodeMajorVersion: 10,
                    predefinedTriggers: [
                        {
                            entryPoint: "generateResizedImage",
                            eventTrigger: {
                                eventType: "google.storage.object.finalize",
                                resource: "projects/_/buckets/${param:IMG_BUCKET}",
                                service: "storage.googleapis.com",
                            },
                            name: "ext-ext-test-generateResizedImage",
                            platform: "gcfv1",
                            regions: ["us-west1"],
                        },
                    ],
                    extension: TEST_EXTENSION,
                    extensionVersion: TEST_EXTENSION_VERSION,
                    codebase: "ext-test",
                },
            },
        ];
        for (const testCase of testCases) {
            it(testCase.desc, async () => {
                const e = new extensionsEmulator_1.ExtensionsEmulator({
                    projectId: "test-project",
                    projectNumber: "1234567",
                    projectDir: ".",
                    extensions: {},
                    aliases: [],
                });
                const result = await e.toEmulatableBackend(testCase.input);
                (0, chai_1.expect)(result).to.deep.equal(testCase.expected);
            });
        }
    });
    describe("installAndBuildSourceCode", () => {
        const extensionPath = "src/test/emulators/extensions/firebase/storage-resize-images@0.1.18";
        it("installs dependecies", () => {
            class DependencyInstallingExtensionsEmulator extends extensionsEmulator_1.ExtensionsEmulator {
                constructor(extensionPath) {
                    super({
                        projectId: "test-project",
                        projectNumber: "1234567",
                        projectDir: ".",
                        extensions: {},
                        aliases: [],
                    });
                    this.installAndBuildSourceCode(extensionPath);
                }
            }
            new DependencyInstallingExtensionsEmulator(extensionPath);
            const nodeModulesFolderExists = (0, node_fs_1.existsSync)(`${extensionPath}/functions/node_modules/firebase-tools`);
            (0, chai_1.expect)(nodeModulesFolderExists).to.be.true;
        }).timeout(60000);
    });
});
//# sourceMappingURL=extensionsEmulator.spec.js.map