Skip to content

Commit 181fa6d

Browse files
test-companion: take the save destination from the approved plan's own file
A plain go on a generate plan still regenerated in a live run. The exact save was gated on the pipeline binding's tm_project and tm_folder, but the bindings the CLI reports carry no inputs, so the gate never opened. The tests used a binding shape the CLI never prints. Now a plain go on a generate plan that recorded its run always takes the exact-save path. The destination comes from the plan's cases file (its top-level `destination`), and `tc tm add --from-file` is called without --project/--folder so it uses that same destination. A plan whose file has no destination saves nothing, says why, and stays approvable. Whatever goes wrong on that path is reported; it never falls back to generating again. A go with edits, and a plan made before plans recorded their run, behave as before. Tests now use the bindings JSON exactly as the CLI prints it and a real-shaped cases file with and without a destination, including the case that failed live. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent aa59121 commit 181fa6d

4 files changed

Lines changed: 152 additions & 53 deletions

File tree

‎test-companion/README.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ someone with write access to reply `go`. Read the plan before you reply.
7070
Test Companion reacts 👀 to a request, or a `go`, once it picks it up.
7171

7272
For `generate`, a plain `go` saves exactly the test cases in the plan to Test
73-
Management: the plan's run keeps them (the `test-companion-cases` artifact,
74-
for 7 days) and nothing is generated again. A `@browserstack go` with changes
75-
generates the cases again with your changes as guidance, and says so. If the
76-
plan's cases can't be found, nothing is saved and you are asked to generate a
77-
fresh plan.
73+
Management, in the project and folder the plan recorded: the plan's run keeps
74+
them (the `test-companion-cases` artifact, for 7 days) and nothing is generated
75+
again. A `@browserstack go` with changes generates the cases again with your
76+
changes as guidance, and says so. If the plan's cases can't be found, or the
77+
plan recorded no Test Management destination (set `tm_project` and `tm_folder`
78+
for generate in the pipeline file), nothing is saved and you are asked to
79+
generate a fresh plan.
7880

7981
Without the GitHub App, Test Companion posts as `github-actions[bot]`, the
8082
account every workflow in your repository shares, and recognises its own plans

‎test-companion/src/approved-cases.mjs‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,38 @@ export const CASES_SUBJECT_FILE = "subject.json"
1919
/**
2020
* How a `go` on a plan is carried out:
2121
* - "exact": save the plan's own cases with tm add, no new run;
22-
* - "unavailable": should be exact, but the plan's cases can't be found (say so, never regenerate);
23-
* - "run": today's behaviour, a real run of the job. `edited` is set when that is because the
24-
* approver changed the plan, which tm add cannot apply.
25-
* Only generate with a Test Management destination is saved exactly; other jobs, and a generate
26-
* with nowhere to save, run as before.
22+
* - "unavailable": should be exact, but there is nothing to fetch (say so, never regenerate);
23+
* - "run": a real run of the job. For generate that only happens when the approver changed the
24+
* plan (`edited`), which tm add cannot apply.
25+
* Where the cases go is read from the plan's own cases file, not from here.
26+
*
27+
* Safety rule: a plain go on a generate plan never becomes "run". Generating again would save
28+
* cases nobody reviewed, so every problem after this point is reported instead.
2729
*/
28-
export function approvedSavePlan({ workflow, replyText, binding, runId } = {}) {
30+
export function approvedSavePlan({ workflow, replyText, runId } = {}) {
2931
if (workflow !== "generate") return { mode: "run" }
30-
const input = binding?.input ?? {}
31-
const project = String(input.tm_project ?? "").trim()
32-
const folder = String(input.tm_folder ?? "").trim()
33-
const saveSetting = String(input["save-to-tm"] ?? "").trim().toLowerCase()
34-
if (!project || !folder || saveSetting === "false") return { mode: "run" }
3532
if (hasApproverEdits(replyText)) return { mode: "run", edited: true }
3633
// Plans made before the run id was recorded have nothing to fetch.
3734
if (!/^\d+$/.test(String(runId ?? ""))) return { mode: "unavailable", reason: "no-run" }
38-
return { mode: "exact", project, folder, runId: String(runId) }
35+
return { mode: "exact", runId: String(runId) }
36+
}
37+
38+
/**
39+
* Where the approved cases go, from the cases file the plan's run kept (its top-level
40+
* `destination`). The CLI writes it only when a Test Management destination is configured.
41+
*/
42+
export function readCasesDestination(casesText) {
43+
let data
44+
try {
45+
data = JSON.parse(String(casesText ?? ""))
46+
} catch {
47+
return { error: "unreadable" }
48+
}
49+
if (!data || typeof data !== "object" || !Array.isArray(data.cases)) return { error: "unreadable" }
50+
const project = String(data.destination?.project ?? "").trim()
51+
const folder = String(data.destination?.folder ?? "").trim()
52+
if (!project || !folder) return { error: "no-destination" }
53+
return { project, folder }
3954
}
4055

4156
/** The plan run's cases artifact from a `GET .../runs/<id>/artifacts` reply, or why there is none. */
@@ -103,6 +118,7 @@ const UNAVAILABLE_WHY = {
103118
unreadable: "the plan's saved cases could not be read",
104119
"other-repo": "the saved cases are not from this repository",
105120
"other-commit": "the saved cases are for a different commit than this plan",
121+
"no-destination": "the plan recorded no Test Management destination, so there is nowhere to save its cases",
106122
"save-failed": "Test Management refused the cases before any were saved",
107123
}
108124

@@ -113,7 +129,9 @@ export function renderCasesUnavailable(reason, detail) {
113129
return [
114130
`I did not save anything: ${why}${extra ? ` (${extra})` : ""}.`,
115131
"",
116-
"I have not generated a new set in its place, because that would save cases nobody reviewed. Ask me to **generate** again and approve the fresh plan.",
132+
reason === "no-destination"
133+
? "I have not generated a new set in its place, because that would save cases nobody reviewed. Set `tm_project` and `tm_folder` for generate in the pipeline file, then ask me to **generate** again and approve the fresh plan."
134+
: "I have not generated a new set in its place, because that would save cases nobody reviewed. Ask me to **generate** again and approve the fresh plan.",
117135
].join("\n")
118136
}
119137

‎test-companion/src/main.mjs‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { findTicket } from "./ticket.mjs"
2424
import { childEnvFor } from "./child-env.mjs"
2525
import {
2626
approvedSavePlan,
27+
readCasesDestination,
2728
pickCasesArtifact,
2829
casesSubject,
2930
checkCasesSubject,
@@ -698,13 +699,12 @@ async function main() {
698699
}
699700

700701
// A plain go on a generate plan saves the plan's own cases; nothing is generated again.
701-
const save = approvedSavePlan({
702-
workflow: plan.workflow,
703-
replyText,
704-
binding: pickBinding(bindings, plan.workflow),
705-
runId: waiting?.runId,
706-
})
707-
if (save.mode === "exact" || save.mode === "unavailable") saveApprovedCases(plan, save, approvalNote)
702+
const save = approvedSavePlan({ workflow: plan.workflow, replyText, runId: waiting?.runId })
703+
if (save.mode === "exact" || save.mode === "unavailable") {
704+
saveApprovedCases(plan, save, approvalNote)
705+
// saveApprovedCases always ends the run. Never fall through to generating again.
706+
process.exit(0)
707+
}
708708
if (save.edited) approvalNote = `${approvalNote} ${EDITED_GO_NOTE}`
709709
} else {
710710
// A large change: the run covers what matters and lists what it skipped.
@@ -1636,6 +1636,15 @@ function saveApprovedCases(plan, save, approvalNote) {
16361636
finish({ ...base, outcome: OUTCOMES.COULD_NOT_RUN, answer: renderCasesUnavailable(reason, detail) }, false)
16371637

16381638
if (save.mode !== "exact") return unavailable(save.reason)
1639+
// Anything unexpected is reported too: a plain go must never turn into a new generate run.
1640+
try {
1641+
saveExactly(plan, save, sha, base, finish, unavailable)
1642+
} catch (e) {
1643+
unavailable("save-failed", redactSecrets(String(e?.message ?? e), secretsNow()))
1644+
}
1645+
}
1646+
1647+
function saveExactly(plan, save, sha, base, finish, unavailable) {
16391648
const picked = pickCasesArtifact(gh(["api", `repos/${REPO}/actions/runs/${save.runId}/artifacts?per_page=100`]), save.runId)
16401649
if (picked.error) return unavailable(picked.error)
16411650

@@ -1645,21 +1654,23 @@ function saveApprovedCases(plan, save, approvalNote) {
16451654
const subject = checkCasesSubject(readOptional(join(dir, CASES_SUBJECT_FILE)), { repo: REPO, sha })
16461655
if (!subject.ok) return unavailable(subject.why)
16471656
const casesFile = join(dir, CASES_DATA_FILE)
1648-
if (!existsSync(casesFile)) return unavailable("unreadable")
1657+
// The destination comes from the approved file itself, so the cases go where the plan said.
1658+
const where = readCasesDestination(readOptional(casesFile))
1659+
if (where.error) return unavailable(where.error)
16491660

1650-
notice(`Test Companion: saving the approved plan's cases from run ${save.runId} to ${save.folder} in ${save.project}.`)
1651-
const r = spawnSync(
1652-
TC_PATH,
1653-
["tm", "add", "--from-file", casesFile, "--project", save.project, "--folder", save.folder, "--json"],
1654-
{ encoding: "utf8", env: childEnvFor(env), timeout: 300_000 },
1655-
)
1661+
notice(`Test Companion: saving the approved plan's cases from run ${save.runId} to ${where.folder} in ${where.project}.`)
1662+
const r = spawnSync(TC_PATH, ["tm", "add", "--from-file", casesFile, "--json"], {
1663+
encoding: "utf8",
1664+
env: childEnvFor(env),
1665+
timeout: 300_000,
1666+
})
16561667
const report = parseTmAddReport(r.stdout)
16571668
if (!report) {
16581669
const why = String(r.stderr ?? "").trim().split("\n").filter(Boolean).at(-1)
16591670
return unavailable("save-failed", redactSecrets(why, secretsNow()))
16601671
}
16611672
const { outcome, spend } = exactSaveOutcome(report, OUTCOMES)
1662-
finish({ ...base, outcome, summary: { ...base.summary, ...exactSaveSummary(report, save) }, answer: renderExactSave(report, save) }, spend)
1673+
finish({ ...base, outcome, summary: { ...base.summary, ...exactSaveSummary(report, where) }, answer: renderExactSave(report, where) }, spend)
16631674
}
16641675

16651676
/** The binding a comment is asking for, or the one bound to the event. */

‎test-companion/test/approved-cases.test.mjs‎

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import assert from "node:assert/strict"
33
import { readFileSync } from "node:fs"
44
import {
55
approvedSavePlan,
6+
readCasesDestination,
67
pickCasesArtifact,
78
casesSubject,
89
checkCasesSubject,
@@ -14,42 +15,97 @@ import {
1415
CASES_ARTIFACT,
1516
} from "../src/approved-cases.mjs"
1617
import { OUTCOMES } from "../src/outcome.mjs"
18+
import { isApprovalOnly } from "../src/intent.mjs"
1719
import { proposalMarkerFor, renderProposal, findProposal } from "../src/report.mjs"
1820

19-
const BINDING = { workflow: "generate", input: { "save-to-tm": "true", tm_project: "Checkout", tm_folder: "Web" } }
21+
// `tc workflow bindings --json`, as the CLI really prints it: no `input`, so a binding can't say
22+
// where cases go. The destination is in the plan's cases file instead.
23+
const REAL_BINDINGS = JSON.parse(
24+
'{"settings":{"ask_team":"","platform":"auto","tests_repo":"","tests_path":"","targets":[],"wcag":"","fail_on_new":false,"visual":false},' +
25+
'"bindings":[{"name":"generate-on-comment","workflow":"generate","on":"pr_comment","mode":"live","approval":"review-before-run","enabled":true}]}',
26+
)
27+
const GENERATE_BINDING = REAL_BINDINGS.bindings[0]
28+
29+
// A generate preview's result.md.json, shaped like the CLI's.
30+
const CASES_FILE = {
31+
version: 1,
32+
workflow: "generate",
33+
preview: true,
34+
generated: 1,
35+
savedToTestManagement: 0,
36+
note: "None reached Test Management — this was a preview run.",
37+
cases: [
38+
{
39+
id: "T001",
40+
title: "First retry uses the base delay",
41+
description: "The first attempt waits exactly the base delay.",
42+
preconditions: "The helper is importable.",
43+
steps: [{ step: "Call the helper with attempt 1", result: "Returns the base delay" }],
44+
priority: "high",
45+
type: "functional",
46+
},
47+
],
48+
}
49+
const withDestination = { ...CASES_FILE, destination: { project: "Checkout", folder: "Web / Cart" } }
2050

2151
describe("approvedSavePlan: a plain go on a generate plan saves the plan's own cases", () => {
22-
test("plain go with a destination and a recorded run: exact save", () => {
23-
assert.deepEqual(approvedSavePlan({ workflow: "generate", replyText: "go", binding: BINDING, runId: "1234567890" }), {
52+
test("reproduces the live failure: real binding shape, plain go, run id present → exact, not run", () => {
53+
assert.equal("input" in GENERATE_BINDING, false)
54+
assert.deepEqual(approvedSavePlan({ workflow: GENERATE_BINDING.workflow, replyText: "go", binding: GENERATE_BINDING, runId: "1234567890" }), {
2455
mode: "exact",
25-
project: "Checkout",
26-
folder: "Web",
2756
runId: "1234567890",
2857
})
2958
})
3059

3160
test("go with edits regenerates, and says it is because of the edits", () => {
32-
assert.deepEqual(approvedSavePlan({ workflow: "generate", replyText: "@browserstack go, drop T003", binding: BINDING, runId: "1" }), {
61+
assert.deepEqual(approvedSavePlan({ workflow: "generate", replyText: "@browserstack go, drop T003", runId: "1" }), {
3362
mode: "run",
3463
edited: true,
3564
})
3665
})
3766

3867
test("a plan with no recorded run is unavailable, never regenerated", () => {
39-
assert.deepEqual(approvedSavePlan({ workflow: "generate", replyText: "go", binding: BINDING, runId: undefined }), {
40-
mode: "unavailable",
41-
reason: "no-run",
42-
})
43-
assert.equal(approvedSavePlan({ workflow: "generate", replyText: "go", binding: BINDING, runId: "12; rm -rf /" }).mode, "unavailable")
68+
assert.deepEqual(approvedSavePlan({ workflow: "generate", replyText: "go", runId: undefined }), { mode: "unavailable", reason: "no-run" })
69+
assert.equal(approvedSavePlan({ workflow: "generate", replyText: "go", runId: "12; rm -rf /" }).mode, "unavailable")
4470
})
4571

46-
test("other jobs, and generate with nowhere to save, run as before", () => {
47-
assert.deepEqual(approvedSavePlan({ workflow: "test-pr", replyText: "go", binding: BINDING, runId: "1" }), { mode: "run" })
48-
assert.deepEqual(approvedSavePlan({ workflow: "generate", replyText: "go", binding: { input: {} }, runId: "1" }), { mode: "run" })
49-
assert.deepEqual(
50-
approvedSavePlan({ workflow: "generate", replyText: "go", binding: { input: { ...BINDING.input, "save-to-tm": "false" } }, runId: "1" }),
51-
{ mode: "run" },
52-
)
72+
test("other jobs run as before", () => {
73+
assert.deepEqual(approvedSavePlan({ workflow: "test-pr", replyText: "go", runId: "1" }), { mode: "run" })
74+
assert.deepEqual(approvedSavePlan({ workflow: "fix", replyText: "go", runId: "1" }), { mode: "run" })
75+
})
76+
77+
test("safety rule: a plain go on a generate plan with a run id never becomes a new generate run", () => {
78+
// The reply as the Action passes it: the @browserstack mention is already stripped
79+
// (plan-run.mjs), so "@browserstack go" arrives as "go".
80+
const plainGoes = ["go", "Go", "GO", "go!", "go.", " go ", "go #123456"]
81+
const bindings = [GENERATE_BINDING, undefined, {}, { input: {} }, { input: { "save-to-tm": "false" } }]
82+
for (const replyText of plainGoes) {
83+
assert.equal(isApprovalOnly(replyText), true, `"${replyText}" should be a plain approval`)
84+
for (const binding of bindings) {
85+
assert.equal(approvedSavePlan({ workflow: "generate", replyText, binding, runId: "77" }).mode, "exact", `"${replyText}" with ${JSON.stringify(binding)}`)
86+
}
87+
}
88+
})
89+
})
90+
91+
describe("readCasesDestination: where the cases go comes from the approved file", () => {
92+
test("the file's destination", () => {
93+
assert.deepEqual(readCasesDestination(JSON.stringify(withDestination)), { project: "Checkout", folder: "Web / Cart" })
94+
})
95+
test("no destination (none configured, or an older CLI): nowhere to save", () => {
96+
assert.deepEqual(readCasesDestination(JSON.stringify(CASES_FILE)), { error: "no-destination" })
97+
assert.deepEqual(readCasesDestination(JSON.stringify({ ...CASES_FILE, destination: { project: "Checkout" } })), { error: "no-destination" })
98+
})
99+
test("not a cases file: unreadable", () => {
100+
assert.deepEqual(readCasesDestination("not json"), { error: "unreadable" })
101+
assert.deepEqual(readCasesDestination(JSON.stringify({ destination: withDestination.destination })), { error: "unreadable" })
102+
assert.deepEqual(readCasesDestination(undefined), { error: "unreadable" })
103+
})
104+
test("no destination says so, saves nothing and does not regenerate", () => {
105+
const text = renderCasesUnavailable("no-destination")
106+
assert.match(text, /I did not save anything: the plan recorded no Test Management destination/)
107+
assert.match(text, /I have not generated a new set/)
108+
assert.match(text, /`tm_project` and `tm_folder`/)
53109
})
54110
})
55111

@@ -173,8 +229,20 @@ describe("the wiring", () => {
173229
test("a plan attaches its cases, and approving one saves them with tm add instead of running again", () => {
174230
assert.match(main, /setOutput\("cases-dir", CASES_DIR\)/)
175231
assert.match(main, /env\.GITHUB_RUN_ID,\n\s*\),\n\s*\)/)
176-
assert.match(main, /"tm", "add", "--from-file", casesFile, "--project", save\.project, "--folder", save\.folder, "--json"/)
177-
assert.match(main, /if \(save\.mode === "exact" \|\| save\.mode === "unavailable"\) saveApprovedCases\(plan, save, approvalNote\)/)
232+
// No --project/--folder: tm add takes the destination from the approved file.
233+
assert.match(main, /\["tm", "add", "--from-file", casesFile, "--json"\]/)
234+
assert.doesNotMatch(main, /binding\?\.input|\.input\?\.tm_/)
235+
})
236+
237+
test("safety rule in the wiring: an exact or unavailable go always ends the run", () => {
238+
assert.match(
239+
main,
240+
/if \(save\.mode === "exact" \|\| save\.mode === "unavailable"\) \{\n\s*saveApprovedCases\(plan, save, approvalNote\)\n[^\n]*\n\s*process\.exit\(0\)\n\s*\}/,
241+
)
242+
const fn = main.slice(main.indexOf("function saveApprovedCases("), main.indexOf("function saveExactly("))
243+
assert.match(fn, /try \{\n\s*saveExactly\(/)
244+
assert.match(fn, /catch \(e\) \{\n\s*unavailable\("save-failed"/)
245+
assert.match(fn, /process\.exit\(0\)/)
178246
})
179247

180248
test("action.yml uploads the cases artifact, and a plan's report and result too", () => {

0 commit comments

Comments
 (0)