-
-
Notifications
You must be signed in to change notification settings - Fork 14
fix: guard against null status in seer API, plan command, and monitor schedule formatting #1104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff36454
fix(seer): guard normalizeAgentStatus against null/undefined status
cursoragent f9cb519
fix(plan): block solution planning when root cause needs user input
cursoragent 6bf6a32
fix(monitor): guard against short schedule arrays in formatSchedule
cursoragent 702264b
Merge branch 'main' into cursor/sentry-cli-bug-fixes-4fe7
jared-outpost[bot] 9be98b1
fix(plan): prompt for root cause selection in the terminal
jared-outpost[bot] 4a9c354
fix(plan): act on the root-cause confirmation instead of discarding it
jared-outpost[bot] cdeb04c
style(seer): wrap normalizeAgentStatus signature to satisfy biome
jared-outpost[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| /** | ||
| * Tests for the `issue plan` command's handling of the | ||
| * WAITING_FOR_USER_RESPONSE root-cause state. | ||
| */ | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| vi.mock("../../../src/commands/issue/utils.js", async (importOriginal) => { | ||
| const actual = | ||
| await importOriginal< | ||
| typeof import("../../../src/commands/issue/utils.js") | ||
| >(); | ||
| return Object.fromEntries( | ||
| Object.entries(actual).map(([key, value]) => [ | ||
| key, | ||
| typeof value === "function" ? vi.fn(value) : value, | ||
| ]) | ||
| ); | ||
| }); | ||
|
|
||
| import { isatty } from "node:tty"; | ||
| import { planCommand } from "../../../src/commands/issue/plan.js"; | ||
| // biome-ignore lint/performance/noNamespaceImport: needed for spyOn mocking | ||
| import * as issueUtils from "../../../src/commands/issue/utils.js"; | ||
| import { triggerSolutionPlanning } from "../../../src/lib/api-client.js"; | ||
| import { CliError } from "../../../src/lib/errors.js"; | ||
| import { logger } from "../../../src/lib/logger.js"; | ||
| import type { AutofixState, RootCause } from "../../../src/types/seer.js"; | ||
|
|
||
| vi.mock("node:tty", () => ({ isatty: vi.fn() })); | ||
|
|
||
| vi.mock("../../../src/lib/api-client.js", () => ({ | ||
| triggerSolutionPlanning: vi.fn(async () => ({})), | ||
| })); | ||
|
|
||
| const PLAN_FLAGS = { json: false, force: false, fresh: false } as const; | ||
|
|
||
| function sampleCause(id: number, description: string): RootCause { | ||
| return { id, description }; | ||
| } | ||
|
|
||
| function waitingState(causes: RootCause[]): AutofixState { | ||
| return { | ||
| status: "WAITING_FOR_USER_RESPONSE", | ||
| run_id: 42, | ||
| steps: [ | ||
| { | ||
| id: "root-cause", | ||
| key: "root_cause_analysis", | ||
| status: "WAITING_FOR_USER_RESPONSE", | ||
| title: "Root cause", | ||
| causes, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| function createMockContext() { | ||
| return { | ||
| stdout: { write: vi.fn(() => true) }, | ||
| stderr: { write: vi.fn(() => true) }, | ||
| cwd: "/tmp", | ||
| }; | ||
| } | ||
|
|
||
| async function runPlan(context: ReturnType<typeof createMockContext>) { | ||
| const func = await planCommand.loader(); | ||
| await func.call(context, PLAN_FLAGS, "IOS-1"); | ||
| } | ||
|
|
||
| describe("issue plan waiting-for-user-response", () => { | ||
| let resolveSpy: ReturnType<typeof vi.spyOn>; | ||
| let analyzeSpy: ReturnType<typeof vi.spyOn>; | ||
| let promptSpy: ReturnType<typeof vi.fn>; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| resolveSpy = vi.spyOn(issueUtils, "resolveOrgAndIssueId"); | ||
| analyzeSpy = vi.spyOn(issueUtils, "ensureRootCauseAnalysis"); | ||
| promptSpy = vi.fn(); | ||
| // The command prompts through a tagged logger, so intercept withTag. | ||
| vi.spyOn(logger, "withTag").mockReturnValue({ | ||
| info: vi.fn(), | ||
| prompt: promptSpy, | ||
| } as unknown as ReturnType<typeof logger.withTag>); | ||
| resolveSpy.mockResolvedValue({ org: "test-org", issueId: "1" }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test("throws a UI hint when not interactive", async () => { | ||
| vi.mocked(isatty).mockReturnValue(false); | ||
| analyzeSpy.mockResolvedValue(waitingState([sampleCause(0, "A cause")])); | ||
|
|
||
| await expect(runPlan(createMockContext())).rejects.toBeInstanceOf(CliError); | ||
| expect(promptSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("confirms the root cause and continues to planning when interactive", async () => { | ||
| vi.mocked(isatty).mockReturnValue(true); | ||
| analyzeSpy.mockResolvedValue( | ||
| waitingState([sampleCause(0, "First"), sampleCause(1, "Second")]) | ||
| ); | ||
| promptSpy.mockResolvedValue(true); | ||
| const solutionSpy = vi | ||
| .spyOn(issueUtils, "pollAutofixState") | ||
| .mockResolvedValue({ status: "COMPLETED", run_id: 42, steps: [] }); | ||
|
|
||
| await runPlan(createMockContext()); | ||
|
|
||
| expect(promptSpy).toHaveBeenCalledWith( | ||
| "Generate a solution plan for it?", | ||
| expect.objectContaining({ type: "confirm" }) | ||
| ); | ||
| expect(triggerSolutionPlanning).toHaveBeenCalledWith("test-org", "1", 42); | ||
| expect(solutionSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("throws when the user declines to continue", async () => { | ||
| vi.mocked(isatty).mockReturnValue(true); | ||
| analyzeSpy.mockResolvedValue(waitingState([sampleCause(0, "A cause")])); | ||
| promptSpy.mockResolvedValue(false); | ||
|
|
||
| await expect(runPlan(createMockContext())).rejects.toBeInstanceOf(CliError); | ||
| expect(triggerSolutionPlanning).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("throws when the user cancels the prompt", async () => { | ||
| vi.mocked(isatty).mockReturnValue(true); | ||
| analyzeSpy.mockResolvedValue(waitingState([sampleCause(0, "A cause")])); | ||
| // consola returns a cancel symbol (non-boolean) when cancelled. | ||
| promptSpy.mockResolvedValue(Symbol("clack:cancel")); | ||
|
|
||
| await expect(runPlan(createMockContext())).rejects.toBeInstanceOf(CliError); | ||
| expect(triggerSolutionPlanning).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| function completedState(): AutofixState { | ||
| return { status: "COMPLETED", run_id: 42, steps: [] }; | ||
| } | ||
|
|
||
| describe("issue plan after solution planning", () => { | ||
| let resolveSpy: ReturnType<typeof vi.spyOn>; | ||
| let analyzeSpy: ReturnType<typeof vi.spyOn>; | ||
| let pollSpy: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(isatty).mockReturnValue(false); | ||
| resolveSpy = vi.spyOn(issueUtils, "resolveOrgAndIssueId"); | ||
| analyzeSpy = vi.spyOn(issueUtils, "ensureRootCauseAnalysis"); | ||
| pollSpy = vi.spyOn(issueUtils, "pollAutofixState"); | ||
| resolveSpy.mockResolvedValue({ org: "test-org", issueId: "1" }); | ||
| analyzeSpy.mockResolvedValue(completedState()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test("returns an existing solution without triggering planning", async () => { | ||
| analyzeSpy.mockResolvedValue({ | ||
| status: "COMPLETED", | ||
| run_id: 42, | ||
| steps: [ | ||
| { | ||
| id: "solution", | ||
| key: "solution", | ||
| status: "COMPLETED", | ||
| title: "Solution", | ||
| solution: [{ title: "Fix it", code_snippet_and_analysis: "do x" }], | ||
| }, | ||
| ], | ||
| } as AutofixState); | ||
|
|
||
| await runPlan(createMockContext()); | ||
|
|
||
| expect(triggerSolutionPlanning).not.toHaveBeenCalled(); | ||
| expect(pollSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("throws when planning ends in ERROR", async () => { | ||
| pollSpy.mockResolvedValue({ status: "ERROR", run_id: 42, steps: [] }); | ||
|
|
||
| await expect(runPlan(createMockContext())).rejects.toThrow( | ||
| /Plan creation failed/ | ||
| ); | ||
| }); | ||
|
|
||
| test("throws when planning ends in CANCELLED", async () => { | ||
| pollSpy.mockResolvedValue({ status: "CANCELLED", run_id: 42, steps: [] }); | ||
|
|
||
| await expect(runPlan(createMockContext())).rejects.toThrow( | ||
| /Plan creation was cancelled/ | ||
| ); | ||
| }); | ||
|
|
||
| test("throws a UI hint when planning still needs user input", async () => { | ||
| pollSpy.mockResolvedValue({ | ||
| status: "WAITING_FOR_USER_RESPONSE", | ||
| run_id: 42, | ||
| steps: [], | ||
| }); | ||
|
|
||
| await expect(runPlan(createMockContext())).rejects.toBeInstanceOf(CliError); | ||
| }); | ||
|
|
||
| test("emits the plan when planning completes", async () => { | ||
| pollSpy.mockResolvedValue({ | ||
| status: "COMPLETED", | ||
| run_id: 42, | ||
| steps: [ | ||
| { | ||
| id: "solution", | ||
| key: "solution", | ||
| status: "COMPLETED", | ||
| title: "Solution", | ||
| solution: [{ title: "Fix it", code_snippet_and_analysis: "do x" }], | ||
| }, | ||
| ], | ||
| } as AutofixState); | ||
|
|
||
| const context = createMockContext(); | ||
| await runPlan(context); | ||
|
|
||
| expect(triggerSolutionPlanning).toHaveBeenCalledWith("test-org", "1", 42); | ||
| expect(pollSpy).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * Tests for monitor list schedule formatting. | ||
| */ | ||
|
|
||
| import { describe, expect, test } from "vitest"; | ||
| import { formatSchedule } from "../../../src/commands/monitor/list.js"; | ||
| import type { SentryMonitor } from "../../../src/types/index.js"; | ||
|
|
||
| function monitor(config: SentryMonitor["config"]): SentryMonitor { | ||
| return { | ||
| id: "1", | ||
| slug: "m", | ||
| name: "Monitor", | ||
| status: "active", | ||
| config, | ||
| }; | ||
| } | ||
|
|
||
| describe("formatSchedule", () => { | ||
| test("returns an empty string when no schedule is configured", () => { | ||
| expect(formatSchedule(monitor(undefined))).toBe(""); | ||
| expect(formatSchedule(monitor({}))).toBe(""); | ||
| }); | ||
|
|
||
| test("renders a crontab schedule verbatim", () => { | ||
| expect(formatSchedule(monitor({ schedule: "0 * * * *" }))).toBe( | ||
| "0 * * * *" | ||
| ); | ||
| }); | ||
|
|
||
| test("renders a well-formed interval schedule", () => { | ||
| expect(formatSchedule(monitor({ schedule: [1, "hour"] }))).toBe( | ||
| "every 1 hour" | ||
| ); | ||
| }); | ||
|
|
||
| test("falls back to the first element for a single-element interval", () => { | ||
| expect(formatSchedule(monitor({ schedule: [5] }))).toBe("5"); | ||
| }); | ||
|
|
||
| test("returns an empty string for an empty interval array", () => { | ||
| expect(formatSchedule(monitor({ schedule: [] }))).toBe(""); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.