|
1 | 1 | import { CLIContext, SystemError, err, ok, signedIn, signedOut } from "@microsoft/teamsfx-api"; |
2 | 2 | import { |
| 3 | + CliQuestionName, |
3 | 4 | CollaborationConstants, |
4 | 5 | CollaborationStateResult, |
5 | 6 | FuncToolChecker, |
@@ -58,6 +59,7 @@ import { addCapabilityCommand } from "../../src/commands/models/addCapability"; |
58 | 59 | import { addPluginCommand } from "../../src/commands/models/addPlugin"; |
59 | 60 | import { entraAppUpdateCommand } from "../../src/commands/models/entraAppUpdate"; |
60 | 61 | import { envResetCommand } from "../../src/commands/models/envReset"; |
| 62 | +import * as listTemplatesModule from "../../src/commands/models/listTemplates"; |
61 | 63 | import { regeneratePluginCommand } from "../../src/commands/models/regeneratePlugin"; |
62 | 64 | import { setCommand } from "../../src/commands/models/set"; |
63 | 65 | import { setSensitivityLabelCommand } from "../../src/commands/models/setSensitivityLabel"; |
@@ -142,6 +144,90 @@ describe("CLI commands", () => { |
142 | 144 | const res = await getCreateCommand().handler!(ctx); |
143 | 145 | assert.isTrue(res.isErr()); |
144 | 146 | }); |
| 147 | + |
| 148 | + it("uses template alias and preset language in non-interactive mode", async () => { |
| 149 | + sandbox.stub(activate, "getFxCore").returns(new FxCore({} as any)); |
| 150 | + const createProjectStub = sandbox |
| 151 | + .stub(FxCore.prototype, "createProject") |
| 152 | + .resolves(ok({ projectPath: "..." })); |
| 153 | + sandbox.stub(featureFlagManager, "getBooleanValue").returns(false); |
| 154 | + sandbox.stub(listTemplatesModule, "listAllTemplates").returns([ |
| 155 | + { |
| 156 | + name: "api-plugin", |
| 157 | + alias: "api-plugin-from-scratch", |
| 158 | + displayName: "API Plugin", |
| 159 | + description: "desc", |
| 160 | + language: "typescript", |
| 161 | + }, |
| 162 | + ] as any); |
| 163 | + |
| 164 | + const ctx: CLIContext = { |
| 165 | + command: { ...getCreateCommand(), fullName: "new" }, |
| 166 | + optionValues: { |
| 167 | + capabilities: "api-plugin-from-scratch", |
| 168 | + nonInteractive: true, |
| 169 | + }, |
| 170 | + globalOptionValues: {}, |
| 171 | + argumentValues: [], |
| 172 | + telemetryProperties: {}, |
| 173 | + }; |
| 174 | + |
| 175 | + const res = await getCreateCommand().handler!(ctx); |
| 176 | + |
| 177 | + assert.isTrue(res.isOk()); |
| 178 | + assert.isTrue(createProjectStub.calledOnce); |
| 179 | + const inputs = createProjectStub.firstCall.args[0] as any; |
| 180 | + assert.equal(inputs["template-name"], "api-plugin"); |
| 181 | + assert.equal(inputs["programming-language"], "typescript"); |
| 182 | + }); |
| 183 | + |
| 184 | + it("keeps capability as template-name when template is not found", async () => { |
| 185 | + sandbox.stub(activate, "getFxCore").returns(new FxCore({} as any)); |
| 186 | + const createProjectStub = sandbox |
| 187 | + .stub(FxCore.prototype, "createProject") |
| 188 | + .resolves(ok({ projectPath: "..." })); |
| 189 | + sandbox.stub(featureFlagManager, "getBooleanValue").returns(false); |
| 190 | + sandbox.stub(listTemplatesModule, "listAllTemplates").returns([] as any); |
| 191 | + |
| 192 | + const ctx: CLIContext = { |
| 193 | + command: { ...getCreateCommand(), fullName: "new" }, |
| 194 | + optionValues: { |
| 195 | + capabilities: "unknown-template", |
| 196 | + nonInteractive: true, |
| 197 | + "programming-language": "javascript", |
| 198 | + }, |
| 199 | + globalOptionValues: {}, |
| 200 | + argumentValues: [], |
| 201 | + telemetryProperties: {}, |
| 202 | + }; |
| 203 | + |
| 204 | + const res = await getCreateCommand().handler!(ctx); |
| 205 | + |
| 206 | + assert.isTrue(res.isOk()); |
| 207 | + const inputs = createProjectStub.firstCall.args[0] as any; |
| 208 | + assert.equal(inputs["template-name"], "unknown-template"); |
| 209 | + assert.equal(inputs["programming-language"], "javascript"); |
| 210 | + }); |
| 211 | + |
| 212 | + it("includes alias and name in capability choices", async () => { |
| 213 | + sandbox.stub(listTemplatesModule, "listAllTemplates").returns([ |
| 214 | + { |
| 215 | + name: "api-plugin", |
| 216 | + alias: "api-plugin-from-scratch", |
| 217 | + displayName: "API Plugin", |
| 218 | + description: "desc", |
| 219 | + language: "typescript", |
| 220 | + }, |
| 221 | + ] as any); |
| 222 | + |
| 223 | + const command = getCreateCommand(); |
| 224 | + const capabilityOption = command.options?.find((o) => o.name === CliQuestionName.Capability); |
| 225 | + |
| 226 | + assert.deepEqual((capabilityOption as any)?.choices, [ |
| 227 | + "api-plugin-from-scratch", |
| 228 | + "api-plugin", |
| 229 | + ]); |
| 230 | + }); |
145 | 231 | }); |
146 | 232 |
|
147 | 233 | describe("createSampleCommand", async () => { |
@@ -1590,6 +1676,32 @@ describe("CLI read-only commands", () => { |
1590 | 1676 | const res = await listTemplatesCommand.handler!(ctx); |
1591 | 1677 | assert.isTrue(res.isOk()); |
1592 | 1678 | }); |
| 1679 | + |
| 1680 | + it("groupTemplatesByName groups by name and falls back display name", async () => { |
| 1681 | + const templates = listTemplatesModule.groupTemplatesByName([ |
| 1682 | + { |
| 1683 | + name: "dup-template", |
| 1684 | + alias: "dup-alias", |
| 1685 | + description: "desc 1", |
| 1686 | + language: "typescript", |
| 1687 | + }, |
| 1688 | + { |
| 1689 | + name: "dup-template", |
| 1690 | + alias: "dup-alias-2", |
| 1691 | + description: "desc 2", |
| 1692 | + language: "javascript", |
| 1693 | + }, |
| 1694 | + { |
| 1695 | + name: "no-alias-template", |
| 1696 | + description: "desc 3", |
| 1697 | + language: "typescript", |
| 1698 | + }, |
| 1699 | + ] as any); |
| 1700 | + |
| 1701 | + assert.equal(templates.length, 2); |
| 1702 | + assert.equal(templates[0].displayName, "dup-alias"); |
| 1703 | + assert.equal(templates[1].displayName, "no-alias-template"); |
| 1704 | + }); |
1593 | 1705 | }); |
1594 | 1706 | describe("listSamplesCommand", async () => { |
1595 | 1707 | it("json", async () => { |
|
0 commit comments