Files
2026-08-04 16:56:03 +10:00

64 lines
2.0 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import type { FilePart } from "@opencode-ai/sdk/v2"
import { attached, inline, kind, typeLabel } from "./message-file"
function file(part: Partial<FilePart> = {}): FilePart {
return {
id: "part_1",
sessionID: "ses_1",
messageID: "msg_1",
type: "file",
mime: "text/plain",
url: "file:///repo/README.txt",
filename: "README.txt",
...part,
}
}
describe("message-file", () => {
test("treats data URLs as attachments", () => {
expect(attached(file({ url: "data:text/plain;base64,SGVsbG8=" }))).toBe(true)
expect(attached(file())).toBe(false)
})
test("keeps data-backed file mentions inline", () => {
expect(
inline(
file({
source: {
type: "file",
path: "/repo/README.txt",
text: { value: "@README.txt", start: 0, end: 11 },
},
}),
),
).toBe(true)
const mentioned = file({
url: "data:text/plain;base64,SGVsbG8=",
source: {
type: "file",
path: "/repo/README.txt",
text: { value: "@README.txt", start: 0, end: 11 },
},
})
expect(inline(mentioned)).toBe(true)
expect(attached(mentioned)).toBe(false)
})
test("separates image and file attachment kinds", () => {
expect(kind(file({ mime: "image/png" }))).toBe("image")
expect(kind(file({ mime: "application/pdf" }))).toBe("file")
})
test("labels attachment types from the basename extension", () => {
expect(typeLabel("list.md", "text/plain", "File")).toBe("Markdown")
expect(typeLabel("/repo/src/main.ts", "text/plain", "File")).toBe("TypeScript")
expect(typeLabel("/tmp/report.pdf", "application/pdf", "File")).toBe("PDF")
expect(typeLabel("notes.xyz", "text/plain", "File")).toBe("XYZ")
expect(typeLabel("/home/user/my.project/Makefile", "text/plain", "File")).toBe("File")
expect(typeLabel(".gitignore", "text/plain", "File")).toBe("File")
expect(typeLabel("/repo/.env", "text/plain", "File")).toBe("File")
})
})