From 525e43355ca40073290f13f265587c38c3fa5569 Mon Sep 17 00:00:00 2001 From: Naimish Srivastava Date: Mon, 23 Nov 2026 16:40:19 +0530 Subject: [PATCH 1/3] test-companion: clearer replies: links instead of ids, a highlighted next step - The report table links "View comment" and "View plan" instead of showing comment numbers. - A plan ends with a highlighted "Next step" box that says what `go` does for that job, how to change the plan first, and when it expires. - Saved test cases link to their Test Management folder. - When approved cases aren't saved, the reply links straight to them on GitHub so nothing is lost. A plan no longer suggests downloading anything. Links are built by the Action from ids of a fixed shape, never read from the run's own summary. Co-Authored-By: Claude Opus 5.5 --- test-companion/src/approved-cases.mjs | 56 +++++-- test-companion/src/main.mjs | 28 +++- test-companion/src/report.mjs | 79 +++++++--- test-companion/test/approved-cases.test.mjs | 8 +- test-companion/test/reply-links.test.mjs | 160 ++++++++++++++++++++ test-companion/test/report.test.mjs | 4 +- test-companion/test/request-link.test.mjs | 13 +- 7 files changed, 296 insertions(+), 52 deletions(-) create mode 100644 test-companion/test/reply-links.test.mjs diff --git a/test-companion/src/approved-cases.mjs b/test-companion/src/approved-cases.mjs index 7762dda..c4419de 100644 --- a/test-companion/src/approved-cases.mjs +++ b/test-companion/src/approved-cases.mjs @@ -194,14 +194,9 @@ export function renderPlannedCases(bytes, { maxChars = 55_000 } = {}) { if (!data || typeof data !== "object" || !Array.isArray(data.cases)) return undefined const cases = data.cases const where = readCasesDestination(JSON.stringify(data)) - const ids = (a, b) => [a, b].filter(Boolean).map(plain).join(", ") const head = where.error ? "**Will be saved to:** nowhere. This plan records no Test Management destination, so approving it can't save these cases." - : `**Will be saved to:** ${plain(where.folder)} in ${plain(where.project)}${ - data.destination?.folderId || data.destination?.projectId - ? ` (${ids(data.destination?.projectId, data.destination?.folderId)})` - : "" - }, once approved.` + : `**Will be saved to:** ${whereLinked(where)}, once approved.` const count = `**${cases.length} test case${cases.length === 1 ? "" : "s"}:**` const unshown = unshownFields(data) const warning = unshown.length @@ -250,7 +245,36 @@ export function readCasesDestination(casesText) { const project = String(data.destination?.project ?? "").trim() const folder = String(data.destination?.folder ?? "").trim() if (!project || !folder) return { error: "no-destination" } - return { project, folder } + const projectId = String(data.destination?.projectId ?? "").trim() + const folderId = String(data.destination?.folderId ?? "").trim() + return { project, folder, ...(projectId ? { projectId } : {}), ...(folderId ? { folderId } : {}) } +} + +/** + * The Test Management page of a folder, or undefined. Built only from ids of the expected shape, + * so nothing in the cases file can turn it into a link elsewhere. + */ +export function tmFolderLink(projectId, folderId) { + const project = String(projectId ?? "") + const folder = String(folderId ?? "") + if (!/^[A-Za-z0-9_-]{1,64}$/.test(project) || !/^\d{1,20}$/.test(folder)) return undefined + return `https://test-management.browserstack.com/projects/${project}/folder/${folder}` +} + +/** GitHub's download page for one artifact of a run in this repository, or undefined. */ +export function artifactDownloadLink(repo, runId, artifactId) { + const r = String(repo ?? "") + const run = String(runId ?? "") + const artifact = String(artifactId ?? "") + if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(r) || !/^\d+$/.test(run) || !/^\d+$/.test(artifact)) return undefined + return `https://github.com/${r}/actions/runs/${run}/artifacts/${artifact}` +} + +// "folder in project", linked to the folder when its ids are known. +function whereLinked({ project, folder, projectId, folderId } = {}) { + const where = `${plain(folder)} in ${plain(project)}` + const url = tmFolderLink(projectId, folderId) + return url ? `[${where}](${url})` : where } /** @@ -371,8 +395,11 @@ const UNAVAILABLE_WHY = { "lookup-failed": "I couldn't look up the plan's saved cases on GitHub just now", } -/** The reply when the approved cases can't be saved as they are. Never regenerates. */ -export function renderCasesUnavailable(reason, detail) { +/** + * The reply when the approved cases can't be saved as they are. Never regenerates. `download` is + * the plan's cases artifact, given only when those cases are the approved ones. + */ +export function renderCasesUnavailable(reason, detail, { download } = {}) { const why = UNAVAILABLE_WHY[reason] ?? "the plan's cases could not be used" const extra = String(detail ?? "").trim() const notInstead = "I have not generated a new set in its place, because that would save cases nobody reviewed." @@ -384,10 +411,12 @@ export function renderCasesUnavailable(reason, detail) { "old-cli": `${notInstead} ${update} Then reply **go** to this plan again; it is still waiting.`, "old-cli-plan": `${notInstead} ${update} Then ask me to **generate** again and approve the fresh plan.`, } + const keep = download ? ["", `The approved test cases aren't lost: [download them](${download}).`] : [] return [ `I did not save anything: ${why}${extra ? ` (${extra})` : ""}.`, "", next[reason] ?? `${notInstead} Ask me to **generate** again and approve the fresh plan.`, + ...keep, ].join("\n") } @@ -407,12 +436,12 @@ export function browserstackLink(url) { } /** The answer text for an exact save: what was created, with ids, and anything that failed. */ -export function renderExactSave(report, { project, folder } = {}) { +export function renderExactSave(report, destination = {}, { download } = {}) { const created = report?.created ?? [] const failed = report?.failed ?? [] const skipped = report?.skipped ?? [] const lines = [] - const where = `${plain(folder)} in ${plain(project)}` + const where = whereLinked(destination) if (created.length > 0) { lines.push(`Saved the ${created.length} approved test case${created.length === 1 ? "" : "s"} to ${where} exactly as planned; nothing was generated again.`) lines.push("") @@ -428,6 +457,7 @@ export function renderExactSave(report, { project, folder } = {}) { lines.push(`**${failed.length} could not be saved:**`) lines.push("") for (const f of failed) lines.push(`- ${plain(f.title)}: ${plain(f.error)}`) + if (download) lines.push("", `[Download the approved test cases](${download}) to keep the ones that weren't saved.`) } if (skipped.length > 0) { if (lines.length) lines.push("") @@ -469,8 +499,8 @@ export function exactSaveOutcome(report, outcomes) { * report). Some cases may be in Test Management, so the plan is used up: approving it again * could save them twice. */ -export function renderSaveInterrupted({ project, folder } = {}, why) { - const where = project && folder ? `${plain(folder)} in ${plain(project)}` : "Test Management" +export function renderSaveInterrupted(destination = {}, why) { + const where = destination.project && destination.folder ? whereLinked(destination) : "Test Management" const extra = String(why ?? "").trim() return [ `The save stopped before it reported what it saved${extra ? ` (${plain(extra)})` : ""}, so some of the approved cases may already be in ${where}.`, diff --git a/test-companion/src/main.mjs b/test-companion/src/main.mjs index 2138d8b..4ee65d1 100644 --- a/test-companion/src/main.mjs +++ b/test-companion/src/main.mjs @@ -38,6 +38,8 @@ import { parseTmAddReport, renderCasesUnavailable, renderExactSave, + tmFolderLink, + artifactDownloadLink, exactSaveSummary, exactSaveOutcome, EDITED_GO_NOTE, @@ -1092,7 +1094,8 @@ async function main() { const isGenerate = (plan.workflow ?? summary.workflow) === "generate" const casesBytes = isGenerate ? readBytes(join(CASES_DIR, CASES_DATA_FILE)) : undefined // The cases get most of the comment; the run's own report above them is kept short. - const summaryShown = casesBytes ? renderSummaryWithin(view, PLAN_SUMMARY_LIMIT) : renderCheckSummary(view) + const planView = { ...view, proposing: true } + const summaryShown = casesBytes ? renderSummaryWithin(planView, PLAN_SUMMARY_LIMIT) : renderCheckSummary(planView) const planned = casesBytes ? renderPlannedCases(casesBytes, { maxChars: COMMENT_LIMIT - summaryShown.length - sizeNote.length - PROPOSAL_OVERHEAD }) : undefined @@ -1806,8 +1809,8 @@ function saveApprovedCases(plan, save, approvalNote) { if (plan.context.pr) postComment(plan.context.pr, { ...view, consumedMarker: spend ? consumedMarker : "" }) process.exit(0) } - const unavailable = (reason, detail) => - finish({ ...base, outcome: OUTCOMES.COULD_NOT_RUN, answer: renderCasesUnavailable(reason, detail) }, false) + const unavailable = (reason, detail, download) => + finish({ ...base, outcome: OUTCOMES.COULD_NOT_RUN, answer: renderCasesUnavailable(reason, detail, { download }) }, false) if (save.mode !== "exact") return unavailable(save.reason) // Anything unexpected is reported too: a plain go must never turn into a new generate run. @@ -1848,9 +1851,11 @@ function saveExactly(plan, save, sha, base, finish, unavailable) { } const unshown = unshownFields(data) if (unshown.length) return unavailable("unshown", unshown.slice(0, 10).join(", ")) + // From here the file is exactly the approved one, so a save that doesn't happen links to it. + const download = artifactDownloadLink(REPO, save.runId, picked.artifact?.id) // The destination comes from the approved file itself, so the cases go where the plan said. const where = readCasesDestination(bytes.toString("utf8")) - if (where.error === "no-destination") return unavailable(missingDestinationReason(subject.cli), subject.cli || undefined) + if (where.error === "no-destination") return unavailable(missingDestinationReason(subject.cli), subject.cli || undefined, download) if (where.error) return unavailable(where.error) notice(`Test Companion: saving the approved plan's cases from run ${save.runId} to ${where.folder} in ${where.project}.`) @@ -1868,13 +1873,24 @@ function saveExactly(plan, save, sha, base, finish, unavailable) { // Approvable again only when nothing can have been saved. A save that was killed, timed // out or crashed may have saved some, so the plan is used up: a second go could save them twice. if (tmAddStop({ status: r.status, signal: r.signal, errorCode: r.error?.code, stderr }) === "nothing-saved") { - return unavailable("save-failed", why) + return unavailable("save-failed", why, download) } return finish({ ...base, outcome: OUTCOMES.RUN_BROKE, answer: renderSaveInterrupted(where, why) }, true) } if (report.failed.length > 0) logRedacted("tc tm add", stderr) const { outcome, spend } = exactSaveOutcome(report, OUTCOMES) - finish({ ...base, outcome, summary: { ...base.summary, ...exactSaveSummary(report, where) }, answer: renderExactSave(report, where) }, spend) + const someLost = report.failed.length > 0 + finish( + { + ...base, + outcome, + summary: { ...base.summary, ...exactSaveSummary(report, where) }, + answer: renderExactSave(report, where, { download: someLost ? download : undefined }), + savedToUrl: tmFolderLink(where.projectId, where.folderId), + casesDownloadUrl: someLost ? download : undefined, + }, + spend, + ) } /** The binding a comment is asking for, or the one bound to the event. */ diff --git a/test-companion/src/report.mjs b/test-companion/src/report.mjs index 8551355..9915b41 100644 --- a/test-companion/src/report.mjs +++ b/test-companion/src/report.mjs @@ -8,20 +8,22 @@ const withMark = (outcome) => { return mark ? `${mark} ${outcome}` : String(outcome ?? "") } -/** `[#]()` for the comment that asked for this run; "" unless the URL names a comment. */ -export function requestLink(url) { +/** `[View comment]()` for a comment on this PR; "" unless the URL names a comment. */ +export function requestLink(url, label = "View comment") { const u = String(url ?? "") - const id = /^https:\/\/[^\s()|<>[\]]+#issuecomment-(\d+)$/.exec(u)?.[1] - return id ? `[#${id}](${u})` : "" + return /^https:\/\/[^\s()|<>[\]]+#issuecomment-\d+$/.test(u) ? `[${label}](${u})` : "" } -/** `@ | [#]()`; the pipe is escaped so it stays inside the table cell. */ +/** `@ · [View comment]()`. */ export function askedByCell(askedBy, askedAt) { if (!askedBy) return "" const link = requestLink(askedAt) - return link ? `@${askedBy} \\| ${link}` : `@${askedBy}` + return link ? `@${askedBy} · ${link}` : `@${askedBy}` } +// Link text we did not write (a folder or project name): no Markdown or table breakers. +const linkText = (s) => String(s ?? "").replace(/[\\`*_[\]()<>|]/g, "\\$&") + /** * Only comments posted by our own bot login count. Another App or workflow * could post our markers, and a `go` would then run their text as the plan. @@ -39,25 +41,37 @@ export function requestMarkerFor(requestId) { const previewRun = (summary) => summary?.preview === "true" -// Flags generated-but-unsaved cases loudly; bare counts read as success. -function producedLines(summary, resultArtifact) { +/** + * Flags generated-but-unsaved cases loudly; bare counts read as success. The links are built by + * the Action (`savedToUrl`, `casesDownloadUrl`), never read from the run's summary file, which + * the agent can write. + */ +function producedLines(summary, resultArtifact, { proposing, runUrl, savedToUrl, casesDownloadUrl } = {}) { const generated = Number(summary.cases_generated ?? 0) const saved = Number(summary.cases_saved ?? 0) if (!generated && !saved) return [] + // A plan lists its cases below and says where they go; nothing is saved until it is approved. + if (proposing) return [`**Test cases:** ${generated} generated. Nothing is saved until you approve.`, ""] + const lines = [`**Test cases:** ${generated} generated, ${saved} saved to Test Management.`] if (saved > 0 && summary.saved_to) { - lines.push(`They are in \`${inertMarkers(summary.saved_to)}\`.`) - } - if (saved < generated) { lines.push( - saved === 0 - ? "**Nothing was written to Test Management.** Download the result below to keep the cases." - : "Some of them were not saved. Download the result below for the full list.", + savedToUrl + ? `They are in [${linkText(inertMarkers(summary.saved_to))}](${savedToUrl}).` + : `They are in \`${inertMarkers(summary.saved_to)}\`.`, ) } - if (resultArtifact) { - lines.push(`The full list is in the **${resultArtifact}** artifact on this run.`) + if (saved < generated) { + const get = casesDownloadUrl + ? `[Download the test cases](${casesDownloadUrl}) to keep them.` + : runUrl + ? `Download them from the **Artifacts** section of the [run page](${runUrl}) to keep them.` + : "Download the result below to keep the cases." + lines.push(saved === 0 ? `**Nothing was written to Test Management.** ${get}` : `Some of them were not saved. ${get}`) + if (resultArtifact && !casesDownloadUrl) { + lines.push(`The full list is in the **${resultArtifact}** artifact on this run.`) + } } return [lines.join(" "), ""] } @@ -109,6 +123,9 @@ export function renderJobSummary({ errorLine, buildNote, planWorkflow, + proposing, + savedToUrl, + casesDownloadUrl, } = {}) { const lines = [] lines.push(`## Test Companion — ${withMark(outcome)}`) @@ -162,7 +179,7 @@ export function renderJobSummary({ ["Cost", summary.credits ? `${summary.credits} credits` : ""], ["CLI", cliVersion], ["Asked by", askedByCell(askedBy, askedAt)], - ["Plan", approvedPlan?.url ? `[#${approvedPlan.id}](${approvedPlan.url})` : ""], + ["Plan", requestLink(approvedPlan?.url, "View plan")], ].filter(([, v]) => v) lines.push("| | |", "|---|---|") @@ -174,7 +191,7 @@ export function renderJobSummary({ } lines.push(...failureLines(failedTests)) - lines.push(...producedLines(summary, resultArtifact)) + lines.push(...producedLines(summary, resultArtifact, { proposing, runUrl, savedToUrl, casesDownloadUrl })) if (String(workNote ?? "").trim()) { lines.push(inertMarkers(workNote), "") @@ -336,16 +353,30 @@ export function isApprovableJob(workflow, enabledWorkflows) { return job !== "" && !job.startsWith("-") && (enabledWorkflows ?? []).includes(job) } -export function renderProposal(body, sha, requester, workflow, runId, pinned = {}) { +// What `go` does, per job, for the plan's call to action. +const GO_DOES = { + generate: "save these test cases to Test Management", + fix: "apply this fix as a draft pull request", + "test-pr": "write these tests to this branch", + automate: "write these automated tests as a pull request", +} + +/** The plan's call to action, set apart from the report so the next step is obvious. */ +export function renderNextStep(workflow) { + const does = GO_DOES[String(workflow ?? "")] ?? "run this for real" return [ - body, - "", - "Reply **go** to run this for real. Nothing is written until you do. To change the list first, reply **@browserstack go** with your edits. Leave it and it stops being approvable after a day.", - "", - proposalMarkerFor(sha, requester, workflow, runId, pinned), + "> [!IMPORTANT]", + "> **Next step:** reply `go` to " + does + ". Nothing is written until you do.", + ">", + "> - To change the plan first, reply `@browserstack go` followed by your changes.", + "> - To skip it, do nothing: it stops being approvable after a day.", ].join("\n") } +export function renderProposal(body, sha, requester, workflow, runId, pinned = {}) { + return [body, "", renderNextStep(workflow), "", proposalMarkerFor(sha, requester, workflow, runId, pinned)].join("\n") +} + /** "approved", "dropped", or undefined when no plan on this commit was used. */ export function proposalAlreadyUsed(comments, sha, us, workflow) { const want = String(sha ?? "").trim() diff --git a/test-companion/test/approved-cases.test.mjs b/test-companion/test/approved-cases.test.mjs index 253542c..116cdaa 100644 --- a/test-companion/test/approved-cases.test.mjs +++ b/test-companion/test/approved-cases.test.mjs @@ -299,7 +299,7 @@ describe("approving saves exactly the cases the plan showed", () => { test("the plan is shown from the cases file, with where it will be saved", () => { const shown = renderPlannedCases(file) - assert.match(shown.body, /\*\*Will be saved to:\*\* github-app in rag\\_assignment \(PR-42, 7\), once approved\./) + assert.ok(shown.body.includes('**Will be saved to:** [github-app in rag\\_assignment](https://test-management.browserstack.com/projects/PR-42/folder/7), once approved.')) assert.match(shown.body, /\*\*2 test cases:\*\*/) assert.match(shown.body, /
T001 — Base delay<\/summary>/) assert.match(shown.body, /\*\*Priority:\*\* high/) @@ -311,7 +311,9 @@ describe("approving saves exactly the cases the plan showed", () => { test("agent-written text can't mention anyone or add a link", () => { const shown = renderPlannedCases(file).body assert.doesNotMatch(shown, /@octocat/) - assert.doesNotMatch(shown, /\]\(https:\/\//) + // The only link is the destination folder, built from its ids. + const ours = "](https://test-management.browserstack.com/projects/PR-42/folder/7)" + assert.doesNotMatch(shown.replace(ours, ""), /\]\(https:\/\//) assert.doesNotMatch(shown, /https:\/\/evil/) }) @@ -455,7 +457,7 @@ describe("a save that stopped without a report uses the plan up", () => { test("the wiring: killed, timed out or no report is a used-up Run broke; only 'nothing was saved' stays approvable", () => { const main = readFileSync(new URL("../src/main.mjs", import.meta.url), "utf8") const fn = main.slice(main.indexOf("function saveExactly("), main.indexOf("function pickBinding(")) - assert.match(fn, /if \(tmAddStop\(\{ status: r\.status, signal: r\.signal, errorCode: r\.error\?\.code, stderr \}\) === "nothing-saved"\) \{\n\s*return unavailable\("save-failed", why\)/) + assert.match(fn, /if \(tmAddStop\(\{ status: r\.status, signal: r\.signal, errorCode: r\.error\?\.code, stderr \}\) === "nothing-saved"\) \{\n\s*return unavailable\("save-failed", why, download\)/) assert.match(fn, /return finish\(\{ \.\.\.base, outcome: OUTCOMES\.RUN_BROKE, answer: renderSaveInterrupted\(where, why\) \}, true\)/) }) }) diff --git a/test-companion/test/reply-links.test.mjs b/test-companion/test/reply-links.test.mjs new file mode 100644 index 0000000..5502a76 --- /dev/null +++ b/test-companion/test/reply-links.test.mjs @@ -0,0 +1,160 @@ +import { test, describe } from "node:test" +import assert from "node:assert/strict" +import { renderJobSummary, renderNextStep, renderProposal } from "../src/report.mjs" +import { + tmFolderLink, + artifactDownloadLink, + readCasesDestination, + renderCasesUnavailable, + renderExactSave, + renderSaveInterrupted, +} from "../src/approved-cases.mjs" + +const FOLDER = "https://test-management.browserstack.com/projects/PR-164148/folder/61742797" +const DOWNLOAD = "https://github.com/o/r/actions/runs/123/artifacts/456" +const RUN = "https://github.com/o/r/actions/runs/999" +const where = { project: "rag_assignment", folder: "github-app", projectId: "PR-164148", folderId: "61742797" } + +describe("tmFolderLink: the folder's Test Management page, from ids only", () => { + test("a project code and a numeric folder id make the link", () => { + assert.equal(tmFolderLink("PR-164148", "61742797"), FOLDER) + }) + test("anything else gives no link", () => { + assert.equal(tmFolderLink("", "1"), undefined) + assert.equal(tmFolderLink("PR-1", ""), undefined) + assert.equal(tmFolderLink("PR-1/../../x", "1"), undefined) + assert.equal(tmFolderLink("PR-1", "1?next=https://evil.test"), undefined) + assert.equal(tmFolderLink("PR 1", "1"), undefined) + }) +}) + +describe("artifactDownloadLink: GitHub's page for one artifact", () => { + test("repo, run and artifact ids make the link", () => { + assert.equal(artifactDownloadLink("o/r", "123", 456), DOWNLOAD) + }) + test("anything else gives no link", () => { + assert.equal(artifactDownloadLink("o/r", "123", undefined), undefined) + assert.equal(artifactDownloadLink("o", "123", "456"), undefined) + assert.equal(artifactDownloadLink("o/r/x", "123", "456"), undefined) + assert.equal(artifactDownloadLink("o/r", "12a", "456"), undefined) + }) +}) + +describe("readCasesDestination keeps the ids the link needs", () => { + test("ids are passed through when present", () => { + const text = JSON.stringify({ destination: where, cases: [] }) + assert.deepEqual(readCasesDestination(text), where) + }) +}) + +describe("a saved generate links its folder", () => { + const report = { created: [{ identifier: "TC-1", title: "One" }], failed: [], skipped: [] } + + test("the answer names the folder as a link", () => { + assert.match(renderExactSave(report, where), /to \[github-app in rag\\_assignment\]\(https:\/\/test-management\.browserstack\.com\/projects\/PR-164148\/folder\/61742797\) exactly as planned/) + }) + test("without ids it stays plain text", () => { + const text = renderExactSave(report, { project: "rag_assignment", folder: "github-app" }) + assert.match(text, /to github-app in rag\\_assignment exactly as planned/) + assert.doesNotMatch(text, /\]\(https:/) + }) + test("the counts line links the folder too", () => { + const body = renderJobSummary({ + outcome: "Succeeded", + summary: { cases_generated: "1", cases_saved: "1", saved_to: "github-app in rag_assignment" }, + savedToUrl: FOLDER, + }) + assert.ok(body.includes(`They are in [github-app in rag\\_assignment](${FOLDER}).`)) + assert.doesNotMatch(body, /Download/) + }) + test("a link in the agent-writable summary is never used", () => { + const body = renderJobSummary({ + outcome: "Succeeded", + summary: { cases_generated: "1", cases_saved: "1", saved_to: "x", saved_to_url: "https://evil.test" }, + }) + assert.doesNotMatch(body, /evil/) + assert.ok(body.includes("They are in `x`.")) + }) + test("an interrupted save links the folder to check", () => { + assert.match(renderSaveInterrupted(where, "killed"), /Check \[github-app in rag\\_assignment\]\(https:/) + }) +}) + +describe("cases that weren't saved get a download link", () => { + test("none saved: the direct link replaces 'download the result below'", () => { + const body = renderJobSummary({ + outcome: "Run broke", + summary: { cases_generated: "3", cases_saved: "0" }, + casesDownloadUrl: DOWNLOAD, + resultArtifact: "test-companion-result", + runUrl: RUN, + }) + assert.ok(body.includes(`**Nothing was written to Test Management.** [Download the test cases](${DOWNLOAD}) to keep them.`)) + assert.doesNotMatch(body, /artifact on this run/) + }) + test("no direct link: point at the run page's Artifacts", () => { + const body = renderJobSummary({ outcome: "Succeeded", summary: { cases_generated: "3", cases_saved: "1" }, runUrl: RUN }) + assert.ok(body.includes(`Some of them were not saved. Download them from the **Artifacts** section of the [run page](${RUN}) to keep them.`)) + }) + test("all saved: no download advice", () => { + const body = renderJobSummary({ + outcome: "Succeeded", + summary: { cases_generated: "3", cases_saved: "3" }, + resultArtifact: "test-companion-result", + runUrl: RUN, + }) + assert.doesNotMatch(body, /Download|artifact on this run/) + }) + test("a save that couldn't start links the approved cases", () => { + const text = renderCasesUnavailable("save-failed", "project not found", { download: DOWNLOAD }) + assert.ok(text.includes(`The approved test cases aren't lost: [download them](${DOWNLOAD}).`)) + }) + test("without a link the reply is as before", () => { + assert.doesNotMatch(renderCasesUnavailable("changed"), /download them/) + }) + test("some failed: the answer links the approved cases", () => { + const report = { created: [{ identifier: "TC-1", title: "One" }], failed: [{ title: "Two", error: "bad field" }], skipped: [] } + assert.ok(renderExactSave(report, where, { download: DOWNLOAD }).includes(`[Download the approved test cases](${DOWNLOAD})`)) + }) +}) + +describe("a plan says nothing is saved yet, and sets the next step apart", () => { + test("a plan's counts don't offer a download", () => { + const body = renderJobSummary({ + outcome: "Succeeded", + proposing: true, + summary: { cases_generated: "10", cases_saved: "0", preview: "true" }, + resultArtifact: "test-companion-result", + runUrl: RUN, + }) + assert.ok(body.includes("**Test cases:** 10 generated. Nothing is saved until you approve.")) + assert.doesNotMatch(body, /Download|Nothing was written|artifact on this run/) + }) + test("the next step is a highlighted box naming what go does", () => { + const box = renderNextStep("generate") + assert.match(box, /^> \[!IMPORTANT\]\n/) + assert.match(box, /reply `go` to save these test cases to Test Management/) + assert.match(box, /reply `@browserstack go` followed by your changes/) + assert.match(box, /stops being approvable after a day/) + assert.ok(box.split("\n").every((l) => l.startsWith(">"))) + }) + test("each job says what go does; an unknown one says run", () => { + assert.match(renderNextStep("fix"), /apply this fix as a draft pull request/) + assert.match(renderNextStep("test-pr"), /write these tests to this branch/) + assert.match(renderNextStep("anything"), /run this for real/) + }) + test("the proposal ends with the box, then its marker", () => { + const body = renderProposal("the plan", "a".repeat(40), "octocat", "generate", "1") + const box = body.indexOf("> [!IMPORTANT]") + assert.ok(box > body.indexOf("the plan")) + assert.ok(body.indexOf("