Skip to content

Commit 28245e3

Browse files
committed
Better YAML errors and test coverage
1 parent b4a68f5 commit 28245e3

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

src/appdistribution/yaml_helper.spec.ts

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { TestCase } from "./types";
21
import * as jsYaml from "js-yaml";
2+
import { TestCase } from "./types";
33
import { fromYaml, toYaml } from "./yaml_helper";
44
import { expect } from "chai";
55

@@ -23,7 +23,8 @@ const TEST_CASES: TestCase[] = [
2323
},
2424
{
2525
displayName: "minimal-case",
26-
aiInstructions: { steps: [{ goal: "test-goal" }] },
26+
name: "projects/12345/apps/1:12345:android:beef/testCases/minimal-id",
27+
aiInstructions: { steps: [{ goal: "win" }] },
2728
},
2829
];
2930

@@ -34,31 +35,58 @@ const YAML_STRING = `- displayName: test-display-name
3435
- goal: test-goal
3536
hint: test-hint
3637
successCriteria: test-success-criteria
38+
- displayName: minimal-case
39+
id: minimal-id
40+
steps:
41+
- goal: win
3742
`;
3843

39-
const YAML_DATA = {
40-
displayName: "test-display-name",
41-
id: "test-case-id",
42-
prerequisiteTestCaseId: "prerequisite-test-case-id",
43-
steps: [
44-
{
45-
goal: "test-goal",
46-
hint: "test-hint",
47-
successCriteria: "test-success-criteria",
48-
},
49-
],
50-
};
44+
const YAML_DATA = [
45+
{
46+
displayName: "test-display-name",
47+
id: "test-case-id",
48+
prerequisiteTestCaseId: "prerequisite-test-case-id",
49+
steps: [
50+
{
51+
goal: "test-goal",
52+
hint: "test-hint",
53+
successCriteria: "test-success-criteria",
54+
},
55+
],
56+
},
57+
{
58+
displayName: "minimal-case",
59+
id: "minimal-id",
60+
steps: [{ goal: "win" }],
61+
},
62+
];
5163

5264
describe("YamlHelper", () => {
5365
it("converts TestCase[] to YAML string", () => {
54-
const yamlString = toYaml([TEST_CASE]);
66+
const yamlString = toYaml(TEST_CASES);
67+
expect(jsYaml.safeLoad(yamlString)).to.eql(YAML_DATA);
5568
expect(yamlString).to.eq(YAML_STRING); // brittle ¯\_(ツ)_/¯
56-
expect(jsYaml.safeLoad(yamlString)).to.eql([YAML_DATA]);
5769
});
5870

5971
it("converts YAML string to TestCase[]", () => {
6072
const testCases = fromYaml(APP_NAME, YAML_STRING);
61-
expect(testCases).to.eql([TEST_CASE]);
73+
expect(testCases).to.eql(TEST_CASES);
74+
});
75+
76+
it("converts YAML without ID", () => {
77+
const testCases = fromYaml(
78+
APP_NAME,
79+
`- displayName: minimal-case
80+
steps:
81+
- goal: win
82+
`,
83+
);
84+
expect(testCases).to.eql([
85+
{
86+
displayName: "minimal-case",
87+
aiInstructions: { steps: [{ goal: "win" }] },
88+
},
89+
]);
6290
});
6391

6492
it("throws error if displayName is missing", () => {

src/appdistribution/yaml_helper.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { TestCase } from "./types";
21
import * as jsYaml from "js-yaml";
32
import { FirebaseError } from "../error";
3+
import { TestCase } from "./types";
44

55
declare interface YamlStep {
66
goal?: string;
@@ -63,6 +63,9 @@ function checkAllowedKeys(allowedKeys: Set<string>, o: object) {
6363
}
6464

6565
function fromYamlTestCases(appName: string, yamlTestCases: YamlTestCase[]): TestCase[] {
66+
if (!Array.isArray(yamlTestCases)) {
67+
throw new FirebaseError("YAML file must contain a list of test cases.");
68+
}
6669
return yamlTestCases.map((yamlTestCase) => {
6770
checkAllowedKeys(ALLOWED_YAML_TEST_CASE_KEYS, yamlTestCase);
6871
return {

0 commit comments

Comments
 (0)