From 059c25a7521980cd75a0797214dcf2ce122cb73c Mon Sep 17 00:00:00 2001 From: Sonali Deshpande <48232592+sonalideshpandemsft@users.noreply.github.com> Date: Fri, 12 Jul 2024 14:03:49 -0700 Subject: [PATCH] fix(container-loader): Return codeDetails.name (#21849) This PR fixes a minor bug found in this PR: https://github.com/microsoft/FluidFramework/pull/21542/files#r1648285464 --- .../loader/container-loader/src/contracts.ts | 16 +++----- .../src/test/contract.spec.ts | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 packages/loader/container-loader/src/test/contract.spec.ts diff --git a/packages/loader/container-loader/src/contracts.ts b/packages/loader/container-loader/src/contracts.ts index a2feff5a37d..f4a38a124a1 100644 --- a/packages/loader/container-loader/src/contracts.ts +++ b/packages/loader/container-loader/src/contracts.ts @@ -214,15 +214,9 @@ export const getPackageName = ( codeDetails: IFluidCodeDetails | undefined, ): IContainerPackageInfo => { // TODO: use a real type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - let containerPackageName: any; - if (codeDetails && "name" in codeDetails) { - containerPackageName = codeDetails; - } else if (isFluidPackage(codeDetails?.package)) { - containerPackageName = codeDetails?.package.name; - } else { - containerPackageName = codeDetails?.package; - } - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - return { name: containerPackageName }; + // This is the normal path that any modern customer would hit + const containerPackageName: string | undefined = isFluidPackage(codeDetails?.package) + ? codeDetails?.package.name + : codeDetails?.package; + return { name: containerPackageName as string }; }; diff --git a/packages/loader/container-loader/src/test/contract.spec.ts b/packages/loader/container-loader/src/test/contract.spec.ts new file mode 100644 index 00000000000..0c7bad8c9fe --- /dev/null +++ b/packages/loader/container-loader/src/test/contract.spec.ts @@ -0,0 +1,40 @@ +/*! + * Copyright (c) Microsoft Corporation and contributors. All rights reserved. + * Licensed under the MIT License. + */ + +import { strict as assert } from "node:assert"; + +import type { IFluidCodeDetails } from "@fluidframework/container-definitions/internal"; + +import { getPackageName } from "../contracts.js"; + +const sampleCodeDetailsWithFluidPackage: IFluidCodeDetails = { + package: { + name: "fluid-package", + fluid: {}, + }, +}; + +const sampleCodeDetailsWithPackageWithoutName: IFluidCodeDetails = { + package: "simple-package", +}; + +describe("Contract", () => { + describe("getPackageName", () => { + it("should return the package name if isFluidPackage returns true", () => { + const result = getPackageName(sampleCodeDetailsWithFluidPackage); + assert.deepEqual(result, { name: "fluid-package" }); + }); + + it("should return the package as it is if it does not have a name property", () => { + const result = getPackageName(sampleCodeDetailsWithPackageWithoutName); + assert.deepEqual(result, { name: "simple-package" }); + }); + + it("should return undefined", () => { + const result = getPackageName(undefined); + assert.deepEqual(result, { name: undefined }); + }); + }); +});