mock.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { describe, expect, test } from "bun:test"
  2. import { bodyText, inputMatch, promptMatch } from "../../e2e/prompt/mock"
  3. function hit(body: Record<string, unknown>) {
  4. return { body }
  5. }
  6. describe("promptMatch", () => {
  7. test("matches token in serialized body", () => {
  8. const match = promptMatch("hello")
  9. expect(match(hit({ messages: [{ role: "user", content: "say hello" }] }))).toBe(true)
  10. expect(match(hit({ messages: [{ role: "user", content: "say goodbye" }] }))).toBe(false)
  11. })
  12. })
  13. describe("inputMatch", () => {
  14. test("matches exact tool input in chat completions body", () => {
  15. const input = { questions: [{ header: "Need input", question: "Pick one" }] }
  16. const match = inputMatch(input)
  17. // The seed prompt embeds JSON.stringify(input) in the user message
  18. const prompt = `Use this JSON input: ${JSON.stringify(input)}`
  19. const body = { messages: [{ role: "user", content: prompt }] }
  20. expect(match(hit(body))).toBe(true)
  21. })
  22. test("matches exact tool input in responses API body", () => {
  23. const input = { questions: [{ header: "Need input", question: "Pick one" }] }
  24. const match = inputMatch(input)
  25. const prompt = `Use this JSON input: ${JSON.stringify(input)}`
  26. const body = { model: "test", input: [{ role: "user", content: [{ type: "input_text", text: prompt }] }] }
  27. expect(match(hit(body))).toBe(true)
  28. })
  29. test("matches patchText with newlines", () => {
  30. const patchText = "*** Begin Patch\n*** Add File: test.txt\n+line1\n*** End Patch"
  31. const match = inputMatch({ patchText })
  32. const prompt = `Use this JSON input: ${JSON.stringify({ patchText })}`
  33. const body = { messages: [{ role: "user", content: prompt }] }
  34. expect(match(hit(body))).toBe(true)
  35. // Also works in responses API format
  36. const respBody = { model: "test", input: [{ role: "user", content: [{ type: "input_text", text: prompt }] }] }
  37. expect(match(hit(respBody))).toBe(true)
  38. })
  39. test("does not match unrelated requests", () => {
  40. const input = { questions: [{ header: "Need input" }] }
  41. const match = inputMatch(input)
  42. expect(match(hit({ messages: [{ role: "user", content: "hello" }] }))).toBe(false)
  43. expect(match(hit({ model: "test", input: [] }))).toBe(false)
  44. })
  45. test("does not match partial input", () => {
  46. const input = { questions: [{ header: "Need input", question: "Pick one" }] }
  47. const match = inputMatch(input)
  48. // Only header, missing question
  49. const partial = `Use this JSON input: ${JSON.stringify({ questions: [{ header: "Need input" }] })}`
  50. const body = { messages: [{ role: "user", content: partial }] }
  51. expect(match(hit(body))).toBe(false)
  52. })
  53. })