-
Notifications
You must be signed in to change notification settings - Fork 87
meet-bot: implement POST /send_chat endpoint #25941
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
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
246 changes: 246 additions & 0 deletions
246
skills/meet-join/bot/__tests__/send-chat-endpoint.test.ts
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,246 @@ | ||
| /** | ||
| * Focused tests for the `POST /send_chat` HTTP endpoint. | ||
| * | ||
| * The general HTTP server suite in `http-server.test.ts` covers auth and | ||
| * validation at a high level; this file exercises the full matrix for | ||
| * `/send_chat` specifically — auth, body validation, the 2000-character | ||
| * Meet chat limit, the Playwright failure path (502), and the happy path | ||
| * (200). The `sendChat` helper from `chat-bridge.ts` is mocked via the | ||
| * `onSendChat` callback so no browser is required. | ||
| */ | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | ||
|
|
||
| import { | ||
| createHttpServer, | ||
| type HttpServerHandle, | ||
| } from "../src/control/http-server.js"; | ||
| import { BotState } from "../src/control/state.js"; | ||
|
|
||
| const API_TOKEN = "test-send-chat-token"; | ||
|
|
||
| interface SendChatHarness { | ||
| server: HttpServerHandle; | ||
| receivedText: string[]; | ||
| /** When set, the next `onSendChat` call rejects with this error. */ | ||
| failNextWith: { error: Error | null }; | ||
| } | ||
|
|
||
| function makeServer(): SendChatHarness { | ||
| const receivedText: string[] = []; | ||
| const failNextWith: { error: Error | null } = { error: null }; | ||
| const server = createHttpServer({ | ||
| apiToken: API_TOKEN, | ||
| onLeave: () => {}, | ||
| onSendChat: async (text) => { | ||
| receivedText.push(text); | ||
| if (failNextWith.error) { | ||
| const err = failNextWith.error; | ||
| failNextWith.error = null; | ||
| throw err; | ||
| } | ||
| }, | ||
| onPlayAudio: () => {}, | ||
| }); | ||
| return { server, receivedText, failNextWith }; | ||
| } | ||
|
|
||
| async function startOnRandomPort(server: HttpServerHandle): Promise<string> { | ||
| const { port } = await server.start(0); | ||
| return `http://127.0.0.1:${port}`; | ||
| } | ||
|
|
||
| async function postSendChat( | ||
| base: string, | ||
| body: unknown, | ||
| opts: { auth?: string | null } = {}, | ||
| ): Promise<Response> { | ||
| const headers: Record<string, string> = { | ||
| "content-type": "application/json", | ||
| }; | ||
| if (opts.auth === undefined) { | ||
| headers.authorization = `Bearer ${API_TOKEN}`; | ||
| } else if (opts.auth !== null) { | ||
| headers.authorization = opts.auth; | ||
| } | ||
| return fetch(`${base}/send_chat`, { | ||
| method: "POST", | ||
| headers, | ||
| body: typeof body === "string" ? body : JSON.stringify(body), | ||
| }); | ||
| } | ||
|
|
||
| describe("POST /send_chat endpoint", () => { | ||
| let server: HttpServerHandle | null = null; | ||
|
|
||
| beforeEach(() => { | ||
| BotState.__resetForTests(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| if (server !== null) { | ||
| await server.stop(); | ||
| server = null; | ||
| } | ||
| }); | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // auth | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| test("rejects a request with no authorization header", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| const res = await postSendChat( | ||
| base, | ||
| { type: "send_chat", text: "hello" }, | ||
| { auth: null }, | ||
| ); | ||
| expect(res.status).toBe(401); | ||
| expect(harness.receivedText).toEqual([]); | ||
| }); | ||
|
|
||
| test("rejects a request with the wrong bearer token", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| const res = await postSendChat( | ||
| base, | ||
| { type: "send_chat", text: "hello" }, | ||
| { auth: "Bearer wrong-token" }, | ||
| ); | ||
| expect(res.status).toBe(401); | ||
| expect(harness.receivedText).toEqual([]); | ||
| }); | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // body validation | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| test("rejects a body with the wrong type discriminator with 400", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| const res = await postSendChat(base, { type: "leave", text: "hi" }); | ||
| expect(res.status).toBe(400); | ||
| expect(harness.receivedText).toEqual([]); | ||
| }); | ||
|
|
||
| test("rejects an empty text with 400", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| const res = await postSendChat(base, { type: "send_chat", text: "" }); | ||
| expect(res.status).toBe(400); | ||
| expect(harness.receivedText).toEqual([]); | ||
| }); | ||
|
|
||
| test("rejects a non-JSON body with 400", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| const res = await postSendChat(base, "not json at all"); | ||
| expect(res.status).toBe(400); | ||
| expect(harness.receivedText).toEqual([]); | ||
| }); | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // 2000-char limit | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| test("accepts a message of exactly 2000 characters", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| const text = "a".repeat(2000); | ||
| const res = await postSendChat(base, { type: "send_chat", text }); | ||
| expect(res.status).toBe(200); | ||
| expect(harness.receivedText).toEqual([text]); | ||
| }); | ||
|
|
||
| test("rejects a message of 2001 characters with 400", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| const text = "b".repeat(2001); | ||
| const res = await postSendChat(base, { type: "send_chat", text }); | ||
| expect(res.status).toBe(400); | ||
| const body = (await res.json()) as { error: string; length: number }; | ||
| expect(body.error).toContain("2000"); | ||
| expect(body.length).toBe(2001); | ||
| expect(harness.receivedText).toEqual([]); | ||
| }); | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // happy path | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| test("returns 200 and passes text through to sendChat unchanged", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| const text = "hello from the bot — special chars: \u2603 🎉"; | ||
| const res = await postSendChat(base, { type: "send_chat", text }); | ||
| expect(res.status).toBe(200); | ||
| const body = (await res.json()) as { sent: boolean; timestamp: string }; | ||
| expect(body.sent).toBe(true); | ||
| expect(typeof body.timestamp).toBe("string"); | ||
| // Timestamp should parse as a valid ISO date. | ||
| expect(Number.isNaN(Date.parse(body.timestamp))).toBe(false); | ||
| expect(harness.receivedText).toEqual([text]); | ||
| }); | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Playwright failure path | ||
| // ------------------------------------------------------------------------- | ||
|
|
||
| test("returns 502 when sendChat throws (Playwright selector failure)", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| harness.failNextWith.error = new Error( | ||
| "Timeout 10000ms exceeded waiting for selector", | ||
| ); | ||
|
|
||
| const res = await postSendChat(base, { | ||
| type: "send_chat", | ||
| text: "will fail", | ||
| }); | ||
| expect(res.status).toBe(502); | ||
| const body = (await res.json()) as { sent: boolean; error: string }; | ||
| expect(body.sent).toBe(false); | ||
| expect(body.error).toContain("Timeout"); | ||
| // The handler was invoked before it threw. | ||
| expect(harness.receivedText).toEqual(["will fail"]); | ||
| }); | ||
|
|
||
| test("returns 502 when sendChat rejects with a non-Error value", async () => { | ||
| const harness = makeServer(); | ||
| server = harness.server; | ||
| const base = await startOnRandomPort(server); | ||
|
|
||
| // Force a non-Error throw. `Error` is the shape our code path expects, | ||
| // but Playwright and other libs have been known to throw plain objects, | ||
| // so we verify the stringify fallback behaves. | ||
| harness.failNextWith.error = { toString: () => "weird-failure" } as Error; | ||
|
|
||
| const res = await postSendChat(base, { | ||
| type: "send_chat", | ||
| text: "will fail", | ||
| }); | ||
| expect(res.status).toBe(502); | ||
| const body = (await res.json()) as { sent: boolean; error: string }; | ||
| expect(body.sent).toBe(false); | ||
| expect(body.error).toBe("weird-failure"); | ||
| }); | ||
| }); |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/send_chathandlers run concurrently, but they all drive the same Meet chat input on a single Playwright page. If two authenticated requests overlap, one call canfill()over the other's text beforepress("Enter"), causing the wrong message to be sent (or sent twice) even though both HTTP requests may return success. Adding a per-server mutex/queue aroundonSendChatwould make message delivery deterministic under parallel sends/retries.Useful? React with 👍 / 👎.