Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/loader/container-loader/src/test/attachment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
} from "../attachment.js";
import { combineAppAndProtocolSummary } from "../utils.js";

import type { PartialOrAbsent } from "./failProxy.js";
import { AbsentProperty } from "./failProxy.js";

const emptySummary = combineAppAndProtocolSummary(
{ tree: {}, type: SummaryType.Tree },
{ tree: {}, type: SummaryType.Tree },
Expand Down Expand Up @@ -78,8 +81,8 @@ const createDetachStorage = (
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const createProxyWithFailDefault = <T extends Record<string, any> | undefined>(
partial: Partial<T> = {},
const createProxyWithFailDefault = <T extends Record<string, any>>(
partial: PartialOrAbsent<T> = {},
): T => {
return new Proxy(partial, {
get: (t, p, r): unknown => {
Expand Down Expand Up @@ -202,7 +205,7 @@ describe("runRetriableAttachProcess", () => {
// we have blobs storage, but it is empty,
// so it should be treat like there are no blobs
detachedBlobStorage: createProxyWithFailDefault<
AttachProcessProps["detachedBlobStorage"]
Required<AttachProcessProps>["detachedBlobStorage"]
>({ size: 0 }),
});

Expand All @@ -221,7 +224,7 @@ describe("runRetriableAttachProcess", () => {
await runRetriableAttachProcess(
createProxyWithFailDefault<AttachProcessProps>({
initialAttachmentData: initial,
detachedBlobStorage: undefined,
detachedBlobStorage: AbsentProperty,
createAttachmentSummary: () => {
throw error;
},
Expand Down Expand Up @@ -251,7 +254,7 @@ describe("runRetriableAttachProcess", () => {
initialAttachmentData: initial,
setAttachmentData: (data) => (attachmentData = data),
createAttachmentSummary: () => emptySummary,
detachedBlobStorage: undefined,
detachedBlobStorage: AbsentProperty,
createOrGetStorageService: () => {
throw error;
},
Expand Down
12 changes: 6 additions & 6 deletions packages/loader/container-loader/src/test/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class MockContainer
IDocumentMessage
>;
resolvedUrl?: IResolvedUrl | undefined;
attachState?: AttachState | undefined;
closed?: boolean | undefined = false;
isDirty?: boolean | undefined;
connectionState?: ConnectionState | undefined;
attachState?: AttachState;
closed?: boolean = false;
isDirty?: boolean;
connectionState?: ConnectionState;
connected?: boolean | undefined;
audience?: IAudience | undefined;
audience?: IAudience;
clientId?: string | undefined;
readOnlyInfo?: ReadOnlyInfo | undefined;
readOnlyInfo?: ReadOnlyInfo;

get mockDeltaManager(): MockDeltaManager {
return this.deltaManager as unknown as MockDeltaManager;
Expand Down
20 changes: 19 additions & 1 deletion packages/loader/container-loader/src/test/failProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,35 @@ export const failProxy = <T extends object>(): T => {
return proxy;
};

export const failSometimeProxy = <T extends object>(handler: Partial<T>): T => {
export const AbsentProperty = Symbol("AbsentProperty");

// Allow properties to be explicitly absent to make it easier to create
// partial handlers where proxy won't throw for missing properties, but
// still appear `undefined` when accessed.
export type PartialOrAbsent<T> = {
[P in keyof T]?: T[P] | typeof AbsentProperty;
};

export const failSometimeProxy = <T extends object>(handler: PartialOrAbsent<T>): T => {
const proxy = new Proxy<T>(handler as T, {
get: (t, p, r): unknown => {
if (p === "then") {
return undefined;
}
if (p in handler) {
if (handler[p] === AbsentProperty) {
return undefined;
}
return Reflect.get(t, p, r);
}
throw new Error(`${p.toString()} not implemented`);
},
has: (t, p): boolean => {
if (p in handler) {
return handler[p] !== AbsentProperty;
}
throw new Error(`${p.toString()} not implemented or declared as absent`);
},
});
return proxy;
};
4 changes: 2 additions & 2 deletions packages/loader/container-loader/src/test/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Container } from "../container.js";
import { Loader } from "../loader.js";
import type { IPendingDetachedContainerState } from "../serializedStateManager.js";

import { failProxy, failSometimeProxy } from "./failProxy.js";
import { AbsentProperty, failProxy, failSometimeProxy } from "./failProxy.js";
import {
createTestCodeLoaderProxy,
createTestDocumentServiceFactoryProxy,
Expand All @@ -35,7 +35,7 @@ import {
const documentServiceFactoryFailProxy = failSometimeProxy<
IDocumentServiceFactory & IProvideLayerCompatDetails
>({
ILayerCompatDetails: undefined,
ILayerCompatDetails: AbsentProperty,
});

describe("loader unit test", () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/loader/container-loader/src/test/testProxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "@fluidframework/driver-definitions/internal";
import { v4 as uuid } from "uuid";

import { failSometimeProxy } from "./failProxy.js";
import { AbsentProperty, failSometimeProxy } from "./failProxy.js";

export function createTestDocumentServiceFactoryProxy(
resolvedUrl: IResolvedUrl,
Expand All @@ -38,7 +38,7 @@ export function createTestDocumentServiceFactoryProxy(
createBlob: async () => ({ id: uuid() }),
}),
}),
ILayerCompatDetails: compatibilityDetails,
ILayerCompatDetails: compatibilityDetails ?? AbsentProperty,
});
}

Expand Down Expand Up @@ -74,7 +74,7 @@ export function createTestCodeLoaderProxy(props?: {
}),
disposed: false,
setConnectionState: () => {},
ILayerCompatDetails: props?.layerCompatDetails,
ILayerCompatDetails: props?.layerCompatDetails ?? AbsentProperty,
});
},
},
Expand Down
1 change: 0 additions & 1 deletion packages/loader/container-loader/src/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"outDir": "../../lib/test",
"types": ["mocha", "node"],
"noUncheckedIndexedAccess": false,
"exactOptionalPropertyTypes": false,
},
"include": ["./**/*"],
"references": [
Expand Down
Loading