Skip to content

Commit

Permalink
refactor: manifest fix (#11266)
Browse files Browse the repository at this point in the history
* fix: manifest update

refactor: more

test: ut

* refactor: more

* refactor: more

* test: ut
  • Loading branch information
yuqizhou77 authored Apr 7, 2024
1 parent e1f25db commit a0ea763
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,16 @@ export class CreateAppPackageDriver implements StepDriver {
}

// API plugin
if (manifest.plugins && manifest.plugins.length > 0 && manifest.plugins[0].pluginFile) {
const pluginFile = path.resolve(appDirectory, manifest.plugins[0].pluginFile);
if (manifest.plugins && manifest.plugins.length > 0 && manifest.plugins[0].file) {
const pluginFile = path.resolve(appDirectory, manifest.plugins[0].file);
const checkExistenceRes = await this.validateReferencedFile(pluginFile, appDirectory);
if (checkExistenceRes.isErr()) {
return err(checkExistenceRes.error);
}

const addFileWithVariableRes = await this.addFileWithVariable(
zip,
manifest.plugins[0].pluginFile,
manifest.plugins[0].file,
pluginFile,
TelemetryPropertyKey.customizedAIPluginKeys,
context
Expand All @@ -238,7 +238,7 @@ export class CreateAppPackageDriver implements StepDriver {

const addFilesRes = await this.addPluginRelatedFiles(
zip,
manifest.plugins[0].pluginFile,
manifest.plugins[0].file,
appDirectory,
context
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class ManifestUtils {
manifest: TeamsAppManifest,
manifestPath: string
): Promise<Result<string, FxError>> {
const pluginFile = manifest.plugins?.[0]?.pluginFile;
const pluginFile = manifest.plugins?.[0]?.file;
if (pluginFile) {
const plugin = path.resolve(path.dirname(manifestPath), pluginFile);
const doesFileExist = await fs.pathExists(plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ describe("teamsApp/createAppPackage", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "plugin.json",
file: "plugin.json",
id: "plugin1",
},
];
manifest.icons = {
Expand Down Expand Up @@ -250,7 +251,8 @@ describe("teamsApp/createAppPackage", async () => {
};
manifest.plugins = [
{
pluginFile: "resources/ai-plugin.json",
file: "resources/ai-plugin.json",
id: "plugin1",
},
];
sinon.stub(manifestUtils, "getManifestV3").resolves(ok(manifest));
Expand Down Expand Up @@ -301,7 +303,8 @@ describe("teamsApp/createAppPackage", async () => {
};
manifest.plugins = [
{
pluginFile: "resources/ai-plugin.json",
file: "resources/ai-plugin.json",
id: "plugin1",
},
];
sinon.stub(manifestUtils, "getManifestV3").resolves(ok(manifest));
Expand Down Expand Up @@ -338,7 +341,8 @@ describe("teamsApp/createAppPackage", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "resources/ai-plugin.json",
file: "resources/ai-plugin.json",
id: "plugin1",
},
];
manifest.icons = {
Expand Down Expand Up @@ -369,7 +373,8 @@ describe("teamsApp/createAppPackage", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "resources/ai-plugin.json",
file: "resources/ai-plugin.json",
id: "plugin1",
},
];
manifest.icons = {
Expand Down Expand Up @@ -694,7 +699,8 @@ describe("teamsApp/createAppPackage", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "resources/ai-plugin.json",
file: "resources/ai-plugin.json",
id: "plugin1",
},
];
manifest.icons = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { pluginManifestUtils } from "../../../../src/component/driver/teamsApp/u
import { PluginManifestSchema, TeamsAppManifest, ok } from "@microsoft/teamsfx-api";
import { FileNotFoundError, JSONSyntaxError } from "../../../../src";
import path from "path";
import { AppStudioError } from "../../../../src/component/driver/teamsApp/errors";

describe("pluginManifestUtils", () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -73,7 +74,8 @@ describe("pluginManifestUtils", () => {
validDomains: [],
plugins: [
{
pluginFile: "resources/plugin.json",
file: "resources/plugin.json",
id: "plugin1",
},
],
};
Expand Down Expand Up @@ -140,6 +142,62 @@ describe("pluginManifestUtils", () => {
}
});

it("getApiSpecFilePathFromTeamsManifest error: invalid plugin node case 1", async () => {
const testManifest = {
...teamsManifest,
plugins: [],
};
sandbox.stub(fs, "readFile").resolves(JSON.stringify(pluginManifest) as any);
const res = await pluginManifestUtils.getApiSpecFilePathFromTeamsManifest(
testManifest,
"/test/path"
);
chai.assert.isTrue(res.isErr());

if (res.isErr()) {
chai.assert.equal(res.error.name, AppStudioError.TeamsAppRequiredPropertyMissingError.name);
}
});

it("getApiSpecFilePathFromTeamsManifest error: invalid plugin node case 2", async () => {
const testManifest = {
$schema:
"https://developer.microsoft.com/en-us/json-schemas/teams/v1.9/MicrosoftTeams.schema.json",
manifestVersion: "1.9",
version: "1.0.0",
id: "test",
packageName: "test",
developer: {
name: "test",
websiteUrl: "https://test.com",
privacyUrl: "https://test.com/privacy",
termsOfUseUrl: "https://test.com/termsofuse",
},
icons: {
color: "icon-color.png",
outline: "icon-outline.png",
},
name: {
short: "test",
full: "test",
},
description: {
short: "test",
full: "test",
},
};
sandbox.stub(fs, "readFile").resolves(JSON.stringify(pluginManifest) as any);
const res = await pluginManifestUtils.getApiSpecFilePathFromTeamsManifest(
testManifest as unknown as TeamsAppManifest,
"/test/path"
);
chai.assert.isTrue(res.isErr());

if (res.isErr()) {
chai.assert.equal(res.error.name, AppStudioError.TeamsAppRequiredPropertyMissingError.name);
}
});

it("getApiSpecFilePathFromTeamsManifest error: spec file not exist", async () => {
sandbox.stub(fs, "pathExists").callsFake(async (testPath) => {
if (testPath === path.resolve("/test/resources/openapi.yaml")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,8 @@ describe("listPluginExistingOperations", () => {
...teamsManifest,
plugins: [
{
pluginFile: "resources/plugin.json",
file: "resources/plugin.json",
id: "plugin1",
},
],
};
Expand Down
15 changes: 10 additions & 5 deletions packages/fx-core/tests/core/FxCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,8 @@ describe("copilotPlugin", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin1",
},
];
const listResult: ListAPIResult = {
Expand Down Expand Up @@ -1769,7 +1770,8 @@ describe("copilotPlugin", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin1",
},
];
const listResult: ListAPIResult = {
Expand Down Expand Up @@ -1824,7 +1826,8 @@ describe("copilotPlugin", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin1",
},
];
const listResult: ListAPIResult = {
Expand Down Expand Up @@ -3383,7 +3386,8 @@ describe("copilotPlugin", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin1",
},
];
sinon.stub(manifestUtils, "_readAppManifest").resolves(ok(manifest));
Expand Down Expand Up @@ -3423,7 +3427,8 @@ describe("copilotPlugin", async () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin1",
},
];
sinon.stub(manifestUtils, "_readAppManifest").resolves(ok(manifest));
Expand Down
3 changes: 1 addition & 2 deletions packages/manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ export class ManifestUtil {

if ((manifest as TeamsAppManifest).plugins) {
const apiPlugins = (manifest as TeamsAppManifest).plugins;
if (apiPlugins && apiPlugins.length > 0 && apiPlugins[0].pluginFile)
properties.isPlugin = true;
if (apiPlugins && apiPlugins.length > 0 && apiPlugins[0].file) properties.isPlugin = true;
}

return properties;
Expand Down
3 changes: 2 additions & 1 deletion packages/manifest/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ export interface ITogetherModeScene {
}

export interface IPlugin {
pluginFile: string;
file: string;
id: string;
}

export type AppManifest = Record<string, any>;
Expand Down
1 change: 1 addition & 0 deletions packages/spec-parser/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ export class ConstantString {
static readonly CommandTitleMaxLens = 32;
static readonly ParameterTitleMaxLens = 32;
static readonly SMERequiredParamsMaxNum = 5;
static readonly DefaultPluginId = "plugin_1";
}
3 changes: 2 additions & 1 deletion packages/spec-parser/src/manifestUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export class ManifestUpdater {
const apiPluginRelativePath = ManifestUpdater.getRelativePath(manifestPath, apiPluginFilePath);
manifest.plugins = [
{
pluginFile: apiPluginRelativePath,
file: apiPluginRelativePath,
id: ConstantString.DefaultPluginId,
},
];

Expand Down
12 changes: 8 additions & 4 deletions packages/spec-parser/test/manifestUpdater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ describe("updateManifestWithAiPlugin", () => {
description: { short: "My API", full: "My API description" },
plugins: [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin_1",
},
],
};
Expand Down Expand Up @@ -223,7 +224,8 @@ describe("updateManifestWithAiPlugin", () => {
description: { short: "My API", full: "My API description" },
plugins: [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin_1",
},
],
};
Expand Down Expand Up @@ -364,7 +366,8 @@ describe("updateManifestWithAiPlugin", () => {
description: { short: "My API", full: "My API description" },
plugins: [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin_1",
},
],
};
Expand Down Expand Up @@ -465,7 +468,8 @@ describe("updateManifestWithAiPlugin", () => {
description: { short: "My API", full: "My API description" },
plugins: [
{
pluginFile: "ai-plugin.json",
file: "ai-plugin.json",
id: "plugin_1",
},
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ describe("Api plugin CodeLensProvider", () => {
const manifest = new TeamsAppManifest();
manifest.plugins = [
{
pluginFile: "test.json",
file: "test.json",
id: "plugin1",
},
];
const openApiObject = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"accentColor": "#FFFFFF",
"plugins": [
{
"pluginFile": "ai-plugin.json"
"file": "ai-plugin.json",
"id": "plugin_1"
}
],
"permissions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<ProjectCapability Include="TeamsFx" />
<ProjectCapability Include="APIME" />
<ProjectCapability Include="CopilotPlugin" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"accentColor": "#FFFFFF",
"plugins": [
{
"pluginFile": "ai-plugin.json"
"file": "ai-plugin.json",
"id": "plugin_1"
}
],
"permissions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"accentColor": "#FFFFFF",
"plugins": [
{
"pluginFile": "ai-plugin.json"
"file": "ai-plugin.json",
"id": "plugin_1"
}
],
"permissions": [
Expand Down

0 comments on commit a0ea763

Please sign in to comment.