|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import { tmpdir } from "../fixture/fixture" |
| 4 | +import { sessionTitle } from "../../src/cli/cmd/run" |
| 5 | + |
| 6 | +const entry = path.resolve(import.meta.dir, "../../src/index.ts") |
| 7 | +const TITLE_PROMPT = "Generate a title for this conversation" |
| 8 | + |
| 9 | +describe("sessionTitle (#145)", () => { |
| 10 | + test("--title <text> is used as given", () => { |
| 11 | + expect(sessionTitle({ title: "Release notes", prompt: "Summarise.", message: "Summarise." })).toBe("Release notes") |
| 12 | + }) |
| 13 | + |
| 14 | + test('--title "" keeps the raw message, truncated', () => { |
| 15 | + const message = '"Summarise the changes."\n' |
| 16 | + expect(sessionTitle({ title: "", prompt: "Summarise the changes.", message })).toBe(message) |
| 17 | + expect(sessionTitle({ title: "", prompt: "", message: "x".repeat(60) })).toBe("x".repeat(50) + "...") |
| 18 | + }) |
| 19 | + |
| 20 | + test("defaults to the prompt as typed, without the quoting added for the model", () => { |
| 21 | + const message = '"fix the \\"login\\" bug"\n' |
| 22 | + expect(sessionTitle({ prompt: 'fix the "login" bug', message })).toBe('fix the "login" bug') |
| 23 | + expect(sessionTitle({ prompt: "x".repeat(60), message: "x".repeat(60) })).toBe("x".repeat(50) + "...") |
| 24 | + }) |
| 25 | + |
| 26 | + test("defaults to the trimmed message when the prompt only arrives on stdin", () => { |
| 27 | + expect(sessionTitle({ prompt: "", message: "\nReview this diff.\n" })).toBe("Review this diff.") |
| 28 | + }) |
| 29 | + |
| 30 | + test("truncates by code point, never splitting a surrogate pair", () => { |
| 31 | + const prompt = "a" + "\u{1F600}".repeat(60) |
| 32 | + expect(sessionTitle({ prompt, message: prompt })).toBe("a" + "\u{1F600}".repeat(49) + "...") |
| 33 | + }) |
| 34 | + |
| 35 | + test("defaults to the command and its arguments for command runs", () => { |
| 36 | + expect(sessionTitle({ prompt: "this input", message: '"this input"\n', command: "check" })).toBe( |
| 37 | + "/check this input", |
| 38 | + ) |
| 39 | + expect(sessionTitle({ prompt: "", message: "\n", command: "review" })).toBe("/review") |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +describe("headless run title generation (#145)", () => { |
| 44 | + test.each([ |
| 45 | + ["prompt", ["Check this input."]], |
| 46 | + ["command", ["--command", "check", "this input"]], |
| 47 | + ] as const)( |
| 48 | + "a %s run without --title makes no title-generation request", |
| 49 | + async (_, input) => { |
| 50 | + await using tmp = await tmpdir() |
| 51 | + const requests: string[] = [] |
| 52 | + const server = Bun.serve({ |
| 53 | + hostname: "127.0.0.1", |
| 54 | + port: 0, |
| 55 | + async fetch(request) { |
| 56 | + requests.push(await request.text()) |
| 57 | + const chunk = { |
| 58 | + candidates: [{ index: 0, content: { role: "model", parts: [{ text: "Done." }] }, finishReason: "STOP" }], |
| 59 | + usageMetadata: { promptTokenCount: 7, candidatesTokenCount: 3, totalTokenCount: 10 }, |
| 60 | + } |
| 61 | + return new Response(`data: ${JSON.stringify(chunk)}\n\n`, { |
| 62 | + headers: { "content-type": "text/event-stream" }, |
| 63 | + }) |
| 64 | + }, |
| 65 | + }) |
| 66 | + // Unlike the other run fixtures, the title agent stays enabled, and small_model routes |
| 67 | + // any title-generation request to this fixture server so the test can observe it. |
| 68 | + await Bun.write( |
| 69 | + path.join(tmp.path, "aictrl.json"), |
| 70 | + JSON.stringify({ |
| 71 | + provider: { |
| 72 | + fixture: { |
| 73 | + npm: "@ai-sdk/google", |
| 74 | + options: { apiKey: "fixture", baseURL: `http://127.0.0.1:${server.port}` }, |
| 75 | + models: { "gemini-fixture": { name: "fixture", limit: { context: 100000, output: 1000 } } }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + small_model: "fixture/gemini-fixture", |
| 79 | + command: { check: { template: "Check $ARGUMENTS." } }, |
| 80 | + }), |
| 81 | + ) |
| 82 | + const proc = Bun.spawn( |
| 83 | + [ |
| 84 | + "bun", |
| 85 | + "run", |
| 86 | + "--conditions=browser", |
| 87 | + entry, |
| 88 | + "run", |
| 89 | + "--format", |
| 90 | + "json", |
| 91 | + "--model", |
| 92 | + "fixture/gemini-fixture", |
| 93 | + ...input, |
| 94 | + ], |
| 95 | + { |
| 96 | + cwd: tmp.path, |
| 97 | + env: { |
| 98 | + ...process.env, |
| 99 | + AICTRL_DISABLE_DEFAULT_PLUGINS: "true", |
| 100 | + AICTRL_DISABLE_MODELS_FETCH: "true", |
| 101 | + AICTRL_DISABLE_AUTOCOMPACT: "true", |
| 102 | + }, |
| 103 | + stdin: "ignore", |
| 104 | + stdout: "pipe", |
| 105 | + stderr: "pipe", |
| 106 | + }, |
| 107 | + ) |
| 108 | + const timeout = setTimeout(() => proc.kill("SIGKILL"), 15000) |
| 109 | + try { |
| 110 | + const [stdout, stderr, exit] = await Promise.all([ |
| 111 | + new Response(proc.stdout).text(), |
| 112 | + new Response(proc.stderr).text(), |
| 113 | + proc.exited, |
| 114 | + ]) |
| 115 | + expect(exit, stderr + stdout).toBe(0) |
| 116 | + expect(requests.filter((body) => body.includes(TITLE_PROMPT))).toHaveLength(0) |
| 117 | + expect(requests).toHaveLength(1) |
| 118 | + } finally { |
| 119 | + clearTimeout(timeout) |
| 120 | + proc.kill("SIGKILL") |
| 121 | + server.stop(true) |
| 122 | + } |
| 123 | + }, |
| 124 | + 20000, |
| 125 | + ) |
| 126 | +}) |
0 commit comments