"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const node_assert_1 = require("node:assert");
const internal_1 = require("@fluidframework/core-interfaces/internal");
const internal_2 = require("@fluidframework/core-utils/internal");
const internal_3 = require("@fluidframework/telemetry-utils/internal");
const internal_4 = require("@fluidframework/test-runtime-utils/internal");
const dataStoreContext_js_1 = require("../dataStoreContext.js");
describe("createChildDataStore", () => {
    const throwNYI = () => {
        throw new Error("Method not implemented.");
    };
    const testContext = class TestContext extends dataStoreContext_js_1.FluidDataStoreContext {
        constructor() {
            super(...arguments);
            this.pkg = ["ParentDataStore"];
            this.getInitialSnapshotDetails = throwNYI;
            this.setAttachState = throwNYI;
            this.getAttachSummary = throwNYI;
            this.getAttachGCData = throwNYI;
            this.channel = new Proxy({}, { get: throwNYI });
            this.channelP = new internal_2.LazyPromise(async () => this.channel);
        }
    };
    const createRegistry = (namedEntries) => ({
        get IFluidDataStoreRegistry() {
            return this;
        },
        async get(name) {
            return new Map(namedEntries).get(name);
        },
        getSync(name) {
            const entry = new Map(namedEntries).get(name);
            return (0, internal_2.isPromiseLike)(entry) ? undefined : entry;
        },
    });
    const createContext = (namedEntries) => {
        const registry = createRegistry(namedEntries);
        const createSummarizerNodeFn = () => new Proxy({}, { get: throwNYI });
        const storage = new Proxy({}, { get: throwNYI });
        const parentContext = {
            clientDetails: {
                capabilities: { interactive: true },
            },
            isReadOnly: () => false,
            containerRuntime: {
                createDetachedDataStore(pkg, loadingGroupId) {
                    return new dataStoreContext_js_1.LocalDetachedFluidDataStoreContext({
                        channelToDataStoreFn: (channel) => ({
                            entryPoint: channel.entryPoint,
                            trySetAlias: throwNYI,
                        }),
                        createSummarizerNodeFn,
                        id: "child",
                        makeLocallyVisibleFn: throwNYI,
                        parentContext,
                        pkg,
                        scope: {},
                        snapshotTree: undefined,
                        storage,
                        loadingGroupId,
                    });
                },
            },
            deltaManager: new internal_4.MockDeltaManager(),
        };
        const context = new testContext({
            createSummarizerNodeFn,
            id: "parent",
            parentContext,
            scope: {},
            storage,
        }, false, false, throwNYI);
        context.registry = registry;
        return context;
    };
    const createFactory = (createDataStore) => ({
        type: "ChildDataStore",
        get IFluidDataStoreFactory() {
            return this;
        },
        instantiateDataStore: throwNYI,
        createDataStore,
    });
    it("Child factory does not support synchronous creation", async () => {
        const factory = createFactory();
        const context = createContext([[factory.type, factory]]);
        try {
            context.createChildDataStore(factory);
            node_assert_1.strict.fail("should fail");
        }
        catch (error) {
            (0, node_assert_1.strict)((0, internal_3.isFluidError)(error));
            (0, node_assert_1.strict)(error.errorType === internal_1.FluidErrorTypes.usageError);
            (0, node_assert_1.strict)(error.getTelemetryProperties().noCreateDataStore === true);
        }
    });
    it("Child factory not registered", async () => {
        const factory = createFactory();
        const context = createContext();
        try {
            context.createChildDataStore(factory);
            node_assert_1.strict.fail("should fail");
        }
        catch (error) {
            (0, node_assert_1.strict)((0, internal_3.isFluidError)(error));
            (0, node_assert_1.strict)(error.errorType === internal_1.FluidErrorTypes.usageError);
            (0, node_assert_1.strict)(error.getTelemetryProperties().isUndefined === true);
        }
    });
    it("Child factory is a different instance", async () => {
        const factory = createFactory();
        const context = createContext([[factory.type, createFactory()]]);
        try {
            context.createChildDataStore(factory);
            node_assert_1.strict.fail("should fail");
        }
        catch (error) {
            (0, node_assert_1.strict)((0, internal_3.isFluidError)(error));
            (0, node_assert_1.strict)(error.errorType === internal_1.FluidErrorTypes.usageError);
            (0, node_assert_1.strict)(error.getTelemetryProperties().diffInstance === true);
        }
    });
    it("createChildDataStore", async () => {
        const factory = createFactory(() => ({ runtime: new internal_4.MockFluidDataStoreRuntime() }));
        const context = createContext([[factory.type, factory]]);
        context.createChildDataStore(factory);
    });
});
//# sourceMappingURL=createChildDataStoreSync.spec.js.map