summaryrefslogtreecommitdiffhomepage
path: root/packages/app/test/e2e/mock.test.ts
blob: 3bd80f34dc29d11034b8b88ec85da08f24a22e4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { describe, expect, test } from "bun:test"
import { bodyText, inputMatch, promptMatch } from "../../e2e/prompt/mock"

function hit(body: Record<string, unknown>) {
  return { body }
}

describe("promptMatch", () => {
  test("matches token in serialized body", () => {
    const match = promptMatch("hello")
    expect(match(hit({ messages: [{ role: "user", content: "say hello" }] }))).toBe(true)
    expect(match(hit({ messages: [{ role: "user", content: "say goodbye" }] }))).toBe(false)
  })
})

describe("inputMatch", () => {
  test("matches exact tool input in chat completions body", () => {
    const input = { questions: [{ header: "Need input", question: "Pick one" }] }
    const match = inputMatch(input)

    // The seed prompt embeds JSON.stringify(input) in the user message
    const prompt = `Use this JSON input: ${JSON.stringify(input)}`
    const body = { messages: [{ role: "user", content: prompt }] }
    expect(match(hit(body))).toBe(true)
  })

  test("matches exact tool input in responses API body", () => {
    const input = { questions: [{ header: "Need input", question: "Pick one" }] }
    const match = inputMatch(input)

    const prompt = `Use this JSON input: ${JSON.stringify(input)}`
    const body = { model: "test", input: [{ role: "user", content: [{ type: "input_text", text: prompt }] }] }
    expect(match(hit(body))).toBe(true)
  })

  test("matches patchText with newlines", () => {
    const patchText = "*** Begin Patch\n*** Add File: test.txt\n+line1\n*** End Patch"
    const match = inputMatch({ patchText })

    const prompt = `Use this JSON input: ${JSON.stringify({ patchText })}`
    const body = { messages: [{ role: "user", content: prompt }] }
    expect(match(hit(body))).toBe(true)

    // Also works in responses API format
    const respBody = { model: "test", input: [{ role: "user", content: [{ type: "input_text", text: prompt }] }] }
    expect(match(hit(respBody))).toBe(true)
  })

  test("does not match unrelated requests", () => {
    const input = { questions: [{ header: "Need input" }] }
    const match = inputMatch(input)

    expect(match(hit({ messages: [{ role: "user", content: "hello" }] }))).toBe(false)
    expect(match(hit({ model: "test", input: [] }))).toBe(false)
  })

  test("does not match partial input", () => {
    const input = { questions: [{ header: "Need input", question: "Pick one" }] }
    const match = inputMatch(input)

    // Only header, missing question
    const partial = `Use this JSON input: ${JSON.stringify({ questions: [{ header: "Need input" }] })}`
    const body = { messages: [{ role: "user", content: partial }] }
    expect(match(hit(body))).toBe(false)
  })
})