|
| 1 | +/*! |
| 2 | + * Copyright (c) Microsoft Corporation and contributors. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +import { strict as assert } from "assert"; |
| 7 | + |
| 8 | +import { FluidErrorTypes } from "@fluidframework/core-interfaces/internal"; |
| 9 | +import { isPromiseLike, LazyPromise } from "@fluidframework/core-utils/internal"; |
| 10 | +import { IDocumentStorageService } from "@fluidframework/driver-definitions/internal"; |
| 11 | +import { |
| 12 | + IFluidDataStoreChannel, |
| 13 | + IFluidDataStoreFactory, |
| 14 | + IFluidDataStoreRegistry, |
| 15 | + IFluidParentContext, |
| 16 | + type NamedFluidDataStoreRegistryEntries, |
| 17 | + type IContainerRuntimeBase, |
| 18 | + type ISummarizerNodeWithGC, |
| 19 | +} from "@fluidframework/runtime-definitions/internal"; |
| 20 | +import { isFluidError } from "@fluidframework/telemetry-utils/internal"; |
| 21 | +import { MockFluidDataStoreRuntime } from "@fluidframework/test-runtime-utils/internal"; |
| 22 | + |
| 23 | +import { |
| 24 | + FluidDataStoreContext, |
| 25 | + LocalDetachedFluidDataStoreContext, |
| 26 | +} from "../dataStoreContext.js"; |
| 27 | + |
| 28 | +describe("createChildDataStore", () => { |
| 29 | + const throwNYI = () => { |
| 30 | + throw new Error("Method not implemented."); |
| 31 | + }; |
| 32 | + const testContext = class TestContext extends FluidDataStoreContext { |
| 33 | + protected pkg = ["ParentDataStore"]; |
| 34 | + public registry: IFluidDataStoreRegistry | undefined; |
| 35 | + public getInitialSnapshotDetails = throwNYI; |
| 36 | + public setAttachState = throwNYI; |
| 37 | + public getAttachSummary = throwNYI; |
| 38 | + public getAttachGCData = throwNYI; |
| 39 | + protected channel = new Proxy({} as any as IFluidDataStoreChannel, { get: throwNYI }); |
| 40 | + protected channelP = new LazyPromise(async () => this.channel); |
| 41 | + }; |
| 42 | + |
| 43 | + const createRegistry = ( |
| 44 | + namedEntries?: NamedFluidDataStoreRegistryEntries, |
| 45 | + ): IFluidDataStoreRegistry => ({ |
| 46 | + get IFluidDataStoreRegistry() { |
| 47 | + return this; |
| 48 | + }, |
| 49 | + async get(name) { |
| 50 | + return new Map(namedEntries).get(name); |
| 51 | + }, |
| 52 | + getSync(name) { |
| 53 | + const entry = new Map(namedEntries).get(name); |
| 54 | + return isPromiseLike(entry) ? undefined : entry; |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const createContext = (namedEntries?: NamedFluidDataStoreRegistryEntries) => { |
| 59 | + const registry = createRegistry(namedEntries); |
| 60 | + const createSummarizerNodeFn = () => |
| 61 | + new Proxy({} as any as ISummarizerNodeWithGC, { get: throwNYI }); |
| 62 | + const storage = new Proxy({} as any as IDocumentStorageService, { get: throwNYI }); |
| 63 | + |
| 64 | + const parentContext = { |
| 65 | + clientDetails: { |
| 66 | + capabilities: { interactive: true }, |
| 67 | + }, |
| 68 | + containerRuntime: { |
| 69 | + createDetachedDataStore(pkg, loadingGroupId) { |
| 70 | + return new LocalDetachedFluidDataStoreContext({ |
| 71 | + channelToDataStoreFn: (channel) => ({ |
| 72 | + entryPoint: channel.entryPoint, |
| 73 | + trySetAlias: throwNYI, |
| 74 | + }), |
| 75 | + createSummarizerNodeFn, |
| 76 | + id: "child", |
| 77 | + makeLocallyVisibleFn: throwNYI, |
| 78 | + parentContext, |
| 79 | + pkg, |
| 80 | + scope: {}, |
| 81 | + snapshotTree: undefined, |
| 82 | + storage, |
| 83 | + loadingGroupId, |
| 84 | + }); |
| 85 | + }, |
| 86 | + } satisfies Partial<IContainerRuntimeBase> as unknown as IContainerRuntimeBase, |
| 87 | + } satisfies Partial<IFluidParentContext> as unknown as IFluidParentContext; |
| 88 | + |
| 89 | + const context = new testContext( |
| 90 | + { |
| 91 | + createSummarizerNodeFn, |
| 92 | + id: "parent", |
| 93 | + parentContext, |
| 94 | + scope: {}, |
| 95 | + storage, |
| 96 | + }, |
| 97 | + false, |
| 98 | + false, |
| 99 | + throwNYI, |
| 100 | + ); |
| 101 | + context.registry = registry; |
| 102 | + return context; |
| 103 | + }; |
| 104 | + |
| 105 | + const createFactory = ( |
| 106 | + createDataStore?: IFluidDataStoreFactory["createDataStore"], |
| 107 | + ): IFluidDataStoreFactory => ({ |
| 108 | + type: "ChildDataStore", |
| 109 | + get IFluidDataStoreFactory() { |
| 110 | + return this; |
| 111 | + }, |
| 112 | + instantiateDataStore: throwNYI, |
| 113 | + createDataStore, |
| 114 | + }); |
| 115 | + |
| 116 | + it("Child factory does not support synchronous creation", async () => { |
| 117 | + const factory = createFactory(); |
| 118 | + const context = createContext([[factory.type, factory]]); |
| 119 | + try { |
| 120 | + context.createChildDataStore(factory); |
| 121 | + assert.fail("should fail"); |
| 122 | + } catch (e) { |
| 123 | + assert(isFluidError(e)); |
| 124 | + assert(e.errorType === FluidErrorTypes.usageError); |
| 125 | + assert(e.getTelemetryProperties().noCreateDataStore === true); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + it("Child factory not registered", async () => { |
| 130 | + const factory = createFactory(); |
| 131 | + const context = createContext(); |
| 132 | + try { |
| 133 | + context.createChildDataStore(factory); |
| 134 | + assert.fail("should fail"); |
| 135 | + } catch (e) { |
| 136 | + assert(isFluidError(e)); |
| 137 | + assert(e.errorType === FluidErrorTypes.usageError); |
| 138 | + assert(e.getTelemetryProperties().isUndefined === true); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + it("Child factory is a different instance", async () => { |
| 143 | + const factory = createFactory(); |
| 144 | + const context = createContext([[factory.type, createFactory()]]); |
| 145 | + |
| 146 | + try { |
| 147 | + context.createChildDataStore(factory); |
| 148 | + assert.fail("should fail"); |
| 149 | + } catch (e) { |
| 150 | + assert(isFluidError(e)); |
| 151 | + assert(e.errorType === FluidErrorTypes.usageError); |
| 152 | + assert(e.getTelemetryProperties().diffInstance === true); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + it("createChildDataStore", async () => { |
| 157 | + const factory = createFactory(() => ({ runtime: new MockFluidDataStoreRuntime() })); |
| 158 | + const context = createContext([[factory.type, factory]]); |
| 159 | + context.createChildDataStore(factory); |
| 160 | + }); |
| 161 | +}); |
0 commit comments