|
| 1 | +import { TestHost, BasicTestRunner } from "@typespec/compiler/testing"; |
| 2 | +import { describe, expect, it, beforeEach } from "vitest"; |
| 3 | +import { createTypespecAazTestHost, createTypespecAazTestRunner, compileTypespecAAZOperations } from "./test-aaz.js"; |
| 4 | +import { ModelVar, generateCompileArmResourceTemplate, findObjectsWithKey } from "./util.js"; |
| 5 | + |
| 6 | +describe("discriminator parsing", () => { |
| 7 | + let host: TestHost; |
| 8 | + let runner: BasicTestRunner; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + host = await createTypespecAazTestHost(); |
| 12 | + runner = await createTypespecAazTestRunner(host); |
| 13 | + }); |
| 14 | + |
| 15 | + it("validate discriminator value", async () => { |
| 16 | + const modelTmp: ModelVar = { |
| 17 | + modelKey: "targetValue", |
| 18 | + modelContent: "TargetResourceConfigurations", |
| 19 | + isComposite: true, |
| 20 | + appendModel: ` |
| 21 | + union ResourceKind { |
| 22 | + string, |
| 23 | + FunctionsFlexConsumption: "FunctionsFlexConsumption", |
| 24 | + } |
| 25 | +
|
| 26 | + model FunctionFlexConsumptionResourceConfiguration { |
| 27 | + instanceMemoryMB: int64; |
| 28 | + httpConcurrency?: int64; |
| 29 | + } |
| 30 | + |
| 31 | + @discriminator("kind") |
| 32 | + model TargetResourceConfigurations { |
| 33 | + @visibility(Lifecycle.Create, Lifecycle.Read) |
| 34 | + kind: ResourceKind; |
| 35 | + } |
| 36 | + |
| 37 | + model FunctionFlexConsumptionTargetResourceConfigurations |
| 38 | + extends TargetResourceConfigurations { |
| 39 | + kind: ResourceKind.FunctionsFlexConsumption; |
| 40 | + configurations?: Record<FunctionFlexConsumptionResourceConfiguration>; |
| 41 | + } |
| 42 | + `, |
| 43 | + }; |
| 44 | + |
| 45 | + const code: string = generateCompileArmResourceTemplate(modelTmp); |
| 46 | + const result = await compileTypespecAAZOperations( |
| 47 | + code, |
| 48 | + { |
| 49 | + "operation": "get-resources-operations", |
| 50 | + "api-version": "A", |
| 51 | + "resources": ["/subscriptions/{}/resourcegroups/{}/providers/microsoft.mock/mockresources/{}"], |
| 52 | + }, |
| 53 | + runner, |
| 54 | + ); |
| 55 | + const resultObj = JSON.parse(result!); |
| 56 | + expect(Array.isArray(resultObj)).toBe(true); |
| 57 | + expect(resultObj.length).toBe(1); |
| 58 | + const targetObj = findObjectsWithKey(resultObj[0].pathItem.get.read.http.responses, "targetValue"); |
| 59 | + const anyTypeObj = JSON.stringify(targetObj, null, 2); |
| 60 | + await expect(anyTypeObj).toMatchFileSnapshot("./snapshots/discriminator-prop.json"); |
| 61 | + }); |
| 62 | +}); |
0 commit comments