-
Notifications
You must be signed in to change notification settings - Fork 40
Handle OpenCode question prompts in Discord flow #66
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
Open
fox3000foxy
wants to merge
6
commits into
bevibing:main
Choose a base branch
from
fox3000foxy:fix-question-asked-discord-flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7799467
Handle OpenCode question prompts in Discord flow
9bc60f0
Discord: handle multi-question/multi-select questions as requested ch…
fox3000foxy 8d3ac28
review: fix min_values, session check, deferUpdate, submit validation…
fox3000foxy 745d68a
fix: address 4 remaining review issues
fox3000foxy 74034db
fix: row-based pagination, paginated text, option overflow, test isol…
fox3000foxy 99009f1
fix: isolate serveManager tests from OPENCODE_SERVER_PASSWORD env var
fox3000foxy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,304 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| const sessionManagerMock = vi.hoisted(() => ({ | ||
| getSessionForThread: vi.fn(), | ||
| listQuestions: vi.fn(), | ||
| replyQuestion: vi.fn(), | ||
| rejectQuestion: vi.fn(), | ||
| abortSession: vi.fn(), | ||
| ensureSessionForThread: vi.fn(), | ||
| sendPrompt: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("../services/sessionManager.js", () => sessionManagerMock); | ||
| vi.mock("../services/serveManager.js", () => ({ | ||
| getPort: vi.fn(), | ||
| spawnServe: vi.fn(), | ||
| waitForReady: vi.fn(), | ||
| })); | ||
| vi.mock("../services/dataStore.js", () => ({ | ||
| getChannelModel: vi.fn(), | ||
| getWorktreeMapping: vi.fn(), | ||
| removeWorktreeMapping: vi.fn(), | ||
| })); | ||
| vi.mock("../services/worktreeManager.js", () => ({ | ||
| worktreeExists: vi.fn(), | ||
| removeWorktree: vi.fn(), | ||
| })); | ||
|
|
||
| import { handleButton, handleSelectMenu } from "../handlers/buttonHandler.js"; | ||
|
|
||
| function mockInteraction(customId: string) { | ||
| return { | ||
| customId, | ||
| reply: vi.fn(), | ||
| deferReply: vi.fn(), | ||
| editReply: vi.fn(), | ||
| update: vi.fn().mockResolvedValue(undefined), | ||
| followUp: vi.fn().mockResolvedValue(undefined), | ||
| message: { id: "msg_1" }, | ||
| channel: { id: "channel-1", isThread: () => false }, | ||
| } as any; | ||
| } | ||
|
|
||
| function mockSelectInteraction(customId: string, values: string[]) { | ||
| return { | ||
| customId, | ||
| values, | ||
| reply: vi.fn(), | ||
| deferReply: vi.fn(), | ||
| editReply: vi.fn(), | ||
| update: vi.fn().mockResolvedValue(undefined), | ||
| followUp: vi.fn().mockResolvedValue(undefined), | ||
| message: { id: "msg_1" }, | ||
| channel: { id: "channel-1", isThread: () => false }, | ||
| } as any; | ||
| } | ||
|
|
||
| describe("handleButton question responses", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("answers a single-select question with one question (auto-submit)", async () => { | ||
| sessionManagerMock.getSessionForThread.mockReturnValue({ | ||
| sessionId: "ses_123", | ||
| projectPath: "/repo", | ||
| port: 14098, | ||
| }); | ||
| sessionManagerMock.listQuestions.mockResolvedValue([ | ||
| { | ||
| id: "que_abc", | ||
| sessionID: "ses_123", | ||
| questions: [ | ||
| { | ||
| question: "Approve this plan?", | ||
| options: [{ label: "Approve plan" }, { label: "Revise plan" }], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| sessionManagerMock.replyQuestion.mockResolvedValue(true); | ||
|
|
||
| const interaction = mockInteraction("qanswer:thread123:que_abc:0:0"); | ||
| await handleButton(interaction); | ||
|
|
||
| expect(interaction.update).toHaveBeenCalled(); | ||
| expect(sessionManagerMock.replyQuestion).toHaveBeenCalledWith(14098, "que_abc", [ | ||
| ["Approve plan"], | ||
| ]); | ||
| expect(interaction.followUp).toHaveBeenCalledWith({ | ||
| content: "✅ All questions answered.", | ||
| flags: 64, | ||
| }); | ||
| }); | ||
|
|
||
| it("rejects OpenCode questions", async () => { | ||
| sessionManagerMock.getSessionForThread.mockReturnValue({ | ||
| sessionId: "ses_123", | ||
| projectPath: "/repo", | ||
| port: 14098, | ||
| }); | ||
| sessionManagerMock.rejectQuestion.mockResolvedValue(true); | ||
|
|
||
| const interaction = mockInteraction("qreject:thread123:que_abc"); | ||
| await handleButton(interaction); | ||
|
|
||
| expect(sessionManagerMock.rejectQuestion).toHaveBeenCalledWith(14098, "que_abc"); | ||
| expect(interaction.update).toHaveBeenCalled(); | ||
| expect(interaction.followUp).toHaveBeenCalledWith({ | ||
| content: "🚫 Question rejected.", | ||
| flags: 64, | ||
| }); | ||
| }); | ||
|
|
||
| it("handles multi-question single-select: answers progressively, submits when done", async () => { | ||
| sessionManagerMock.getSessionForThread.mockReturnValue({ | ||
| sessionId: "ses_123", | ||
| projectPath: "/repo", | ||
| port: 14098, | ||
| }); | ||
| sessionManagerMock.listQuestions.mockResolvedValue([ | ||
| { | ||
| id: "que_xyz", | ||
| sessionID: "ses_123", | ||
| questions: [ | ||
| { | ||
| question: "Choose framework?", | ||
| options: [{ label: "React" }, { label: "Vue" }, { label: "Svelte" }], | ||
| }, | ||
| { | ||
| header: "Styling", | ||
| question: "Choose styling approach?", | ||
| options: [{ label: "CSS" }, { label: "Tailwind" }], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| sessionManagerMock.replyQuestion.mockResolvedValue(true); | ||
|
|
||
| const q1Interaction = mockInteraction("qanswer:thread123:que_xyz:0:1"); | ||
| await handleButton(q1Interaction); | ||
|
|
||
| expect(sessionManagerMock.replyQuestion).not.toHaveBeenCalled(); | ||
| expect(q1Interaction.update).toHaveBeenCalled(); | ||
| expect(q1Interaction.followUp).toHaveBeenCalledWith({ | ||
| content: "✅ Q1 answered: Vue", | ||
| flags: 64, | ||
| }); | ||
|
|
||
| const q2Interaction = mockInteraction("qanswer:thread123:que_xyz:1:0"); | ||
| await handleButton(q2Interaction); | ||
|
|
||
| expect(sessionManagerMock.replyQuestion).toHaveBeenCalledWith(14098, "que_xyz", [ | ||
| ["Vue"], | ||
| ["CSS"], | ||
| ]); | ||
| expect(q2Interaction.followUp).toHaveBeenCalledWith({ | ||
| content: "✅ All questions answered.", | ||
| flags: 64, | ||
| }); | ||
| }); | ||
|
|
||
| it("handles multi-select toggle: toggle in, then submit", async () => { | ||
| sessionManagerMock.getSessionForThread.mockReturnValue({ | ||
| sessionId: "ses_123", | ||
| projectPath: "/repo", | ||
| port: 14098, | ||
| }); | ||
| sessionManagerMock.listQuestions.mockResolvedValue([ | ||
| { | ||
| id: "que_multi", | ||
| sessionID: "ses_123", | ||
| questions: [ | ||
| { | ||
| question: "Select toppings?", | ||
| multiple: true, | ||
| options: [ | ||
| { label: "Cheese" }, | ||
| { label: "Pepperoni" }, | ||
| { label: "Mushrooms" }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| sessionManagerMock.replyQuestion.mockResolvedValue(true); | ||
|
|
||
| const toggle1 = mockInteraction("qtoggle:thread123:que_multi:0:0"); | ||
| await handleButton(toggle1); | ||
| expect(sessionManagerMock.replyQuestion).not.toHaveBeenCalled(); | ||
| expect(toggle1.update).toHaveBeenCalled(); | ||
|
|
||
| const toggle2 = mockInteraction("qtoggle:thread123:que_multi:0:2"); | ||
| await handleButton(toggle2); | ||
| expect(sessionManagerMock.replyQuestion).not.toHaveBeenCalled(); | ||
|
|
||
| const submit = mockInteraction("qsubmit:thread123:que_multi"); | ||
| await handleButton(submit); | ||
| expect(sessionManagerMock.replyQuestion).toHaveBeenCalledWith(14098, "que_multi", [ | ||
| ["Cheese", "Mushrooms"], | ||
| ]); | ||
| expect(submit.followUp).toHaveBeenCalledWith({ | ||
| content: "✅ All questions answered.", | ||
| flags: 64, | ||
| }); | ||
| }); | ||
|
|
||
| it("handles select menu interaction for single-select", async () => { | ||
| sessionManagerMock.getSessionForThread.mockReturnValue({ | ||
| sessionId: "ses_123", | ||
| projectPath: "/repo", | ||
| port: 14098, | ||
| }); | ||
| sessionManagerMock.listQuestions.mockResolvedValue([ | ||
| { | ||
| id: "que_select", | ||
| sessionID: "ses_123", | ||
| questions: [ | ||
| { | ||
| question: "Pick one?", | ||
| options: [ | ||
| { label: "Option A" }, | ||
| { label: "Option B" }, | ||
| { label: "Option C" }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| sessionManagerMock.replyQuestion.mockResolvedValue(true); | ||
|
|
||
| const interaction = mockSelectInteraction("qselect:thread123:que_select:0", ["1"]); | ||
| await handleSelectMenu(interaction); | ||
|
|
||
| expect(sessionManagerMock.replyQuestion).toHaveBeenCalledWith(14098, "que_select", [ | ||
| ["Option B"], | ||
| ]); | ||
| expect(interaction.followUp).toHaveBeenCalledWith({ | ||
| content: "✅ All questions answered.", | ||
| flags: 64, | ||
| }); | ||
| }); | ||
|
|
||
| it("handles select menu interaction for multi-select", async () => { | ||
| sessionManagerMock.getSessionForThread.mockReturnValue({ | ||
| sessionId: "ses_123", | ||
| projectPath: "/repo", | ||
| port: 14098, | ||
| }); | ||
| sessionManagerMock.listQuestions.mockResolvedValue([ | ||
| { | ||
| id: "que_multi_select", | ||
| sessionID: "ses_123", | ||
| questions: [ | ||
| { | ||
| question: "Pick multiple?", | ||
| multiple: true, | ||
| options: [ | ||
| { label: "Alpha" }, | ||
| { label: "Beta" }, | ||
| { label: "Gamma" }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| sessionManagerMock.replyQuestion.mockResolvedValue(true); | ||
|
|
||
| const interaction = mockSelectInteraction("qselect:thread123:que_multi_select:0", [ | ||
| "0", | ||
| "2", | ||
| ]); | ||
| await handleSelectMenu(interaction); | ||
|
|
||
| expect(sessionManagerMock.replyQuestion).toHaveBeenCalledWith( | ||
| 14098, | ||
| "que_multi_select", | ||
| [["Alpha", "Gamma"]], | ||
| ); | ||
| expect(interaction.followUp).toHaveBeenCalledWith({ | ||
| content: "✅ All questions answered.", | ||
| flags: 64, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns error for invalid button customId", async () => { | ||
| const interaction = mockInteraction("qanswer:thread123:que_abc:notanumber:0"); | ||
| await handleButton(interaction); | ||
| expect(interaction.reply).toHaveBeenCalledWith({ | ||
| content: "❌ Invalid question response.", | ||
| flags: 64, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns error when session not found", async () => { | ||
| sessionManagerMock.getSessionForThread.mockReturnValue(undefined); | ||
| const interaction = mockInteraction("qanswer:thread123:que_abc:0:0"); | ||
| await handleButton(interaction); | ||
| expect(interaction.reply).toHaveBeenCalledWith({ | ||
| content: "⚠️ Session not found.", | ||
| flags: 64, | ||
| }); | ||
| }); | ||
| }); | ||
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
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.