Files
pipelines/internal/codex/attachments_test.go
T

166 lines
6.0 KiB
Go

package codex
import (
"bytes"
"context"
"encoding/json"
"image"
"image/png"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func preparedFixture(t *testing.T, name string, data []byte) opencode.PromptFile {
t.Helper()
path := filepath.Join(t.TempDir(), name)
if err := os.WriteFile(path, data, 0o600); err != nil {
t.Fatal(err)
}
return opencode.NewPreparedPromptFile((&url.URL{Scheme: "file", Path: path}).String(), name)
}
func TestAttachmentTurnWire(t *testing.T) {
var imageBytes bytes.Buffer
if err := png.Encode(&imageBytes, image.NewRGBA(image.Rect(0, 0, 1, 1))); err != nil {
t.Fatal(err)
}
files := []opencode.PromptFile{
preparedFixture(t, "actual.png", imageBytes.Bytes()),
preparedFixture(t, "ordinary.png", []byte("not an image\x00retain these bytes")),
}
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
if method != "turn/start" {
t.Errorf("unexpected method %s", method)
}
var params struct {
Thread string `json:"threadId"`
Model string `json:"model"`
ClientID string `json:"clientUserMessageId"`
Input []struct {
Type, Text, Name, Path string
Elements []any `json:"text_elements"`
} `json:"input"`
}
if err := json.Unmarshal(raw, &params); err != nil {
t.Error(err)
}
if params.Thread != fixtureThread || params.Model != "fixture-model" || params.ClientID != "msg_files" {
t.Errorf("params = %s", raw)
}
if len(params.Input) != 5 {
t.Errorf("inputs = %s", raw)
return map[string]any{}
}
if params.Input[2].Type != "localImage" || params.Input[4].Type != "mention" || params.Input[4].Name != "ordinary.png" {
t.Errorf("inputs = %s", raw)
}
if !strings.Contains(params.Input[3].Text, "contents are not loaded") {
t.Errorf("missing file semantics: %s", raw)
}
if !strings.Contains(params.Input[3].Text, params.Input[4].Path) || !strings.Contains(params.Input[3].Text, "ordinary.png") {
t.Errorf("file reference is not visible in model text: %s", raw)
}
for _, index := range []int{2, 4} {
if !filepath.IsAbs(params.Input[index].Path) || strings.HasPrefix(params.Input[index].Path, "file:") {
t.Errorf("not a local path: %s", raw)
}
}
if params.Input[0].Elements == nil {
t.Errorf("missing schema text_elements: %s", raw)
}
return map[string]any{}
})
if err := chat.PromptWithMessageIDModelFiles(context.Background(), fixtureThread, "inspect", "msg_files", "fixture-model", files); err != nil {
t.Fatal(err)
}
u, _ := url.Parse(files[1].URI)
data, err := os.ReadFile(u.Path)
if err != nil || string(data) != "not an image\x00retain these bytes" {
t.Fatal("uploaded bytes changed", err)
}
}
func TestAttachmentInputRejectsUnsafeReferences(t *testing.T) {
good := preparedFixture(t, "file.txt", []byte("contents"))
for _, uri := range []string{"", "https://example.com/file", "file://remote/etc/passwd", "file:relative", "file:///etc/passwd?x=1", "file:///etc/passwd#x", "file:///a/../etc/passwd"} {
if _, err := promptInput("hello", []opencode.PromptFile{opencode.NewPreparedPromptFile(uri, "file")}); err == nil {
t.Errorf("accepted %q", uri)
}
}
if _, err := promptInput("hello", []opencode.PromptFile{{URI: good.URI, Name: good.Name}}); err == nil {
t.Fatal("accepted unprepared URI")
}
mutated := good
mutated.URI = "file:///etc/passwd"
if _, err := promptInput("hello", []opencode.PromptFile{mutated}); err == nil {
t.Fatal("accepted mutated URI")
}
oversize := preparedFixture(t, "large", make([]byte, maxPromptFileBytes+1))
if _, err := promptInput("hello", []opencode.PromptFile{oversize}); err == nil {
t.Fatal("accepted oversize file")
}
u, _ := url.Parse(good.URI)
link := filepath.Join(t.TempDir(), "link")
if err := os.Symlink(u.Path, link); err != nil {
t.Fatal(err)
}
if _, err := promptInput("hello", []opencode.PromptFile{opencode.NewPreparedPromptFile((&url.URL{Scheme: "file", Path: link}).String(), "link")}); err == nil {
t.Fatal("accepted symlink")
}
if _, err := promptInput("", []opencode.PromptFile{good}); err != nil {
t.Fatal("file-only prompt rejected", err)
}
if _, err := promptInput("", nil); err == nil {
t.Fatal("empty prompt accepted")
}
if _, err := promptInput("hello", []opencode.PromptFile{good, preparedFixture(t, "combined", make([]byte, maxPromptFileBytes))}); err == nil {
t.Fatal("combined size not bounded")
}
}
func TestUserAttachmentHistory(t *testing.T) {
i := item{Type: "userMessage", ID: "native", ClientID: "client", Content: json.RawMessage(`[{"type":"text","text":"inspect"},{"type":"localImage","path":"/private/image.png"},{"type":"mention","name":"report.txt","path":"/private/report.txt"}]`)}
msg, ok := i.chatMessage(fixtureThread)
if !ok || msg.ID != "client" || len(msg.Parts) != 3 || msg.Parts[1].Type != protocol.ChatPartFile || msg.Parts[2].Name != "report.txt" || msg.Parts[2].URI != "file:///private/report.txt" {
t.Fatalf("history = %+v", msg)
}
}
func TestAttachmentAndPlainPromptWithoutModel(t *testing.T) {
file := preparedFixture(t, "file.txt", []byte("hello"))
calls := 0
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
var params map[string]json.RawMessage
if err := json.Unmarshal(raw, &params); err != nil {
t.Error(err)
}
if method != "turn/start" || params["model"] != nil {
t.Errorf("unexpected operation/override: %s %s", method, raw)
}
var input []map[string]any
if err := json.Unmarshal(params["input"], &input); err != nil {
t.Error(err)
}
calls++
if calls == 1 && (len(input) != 1 || input[0]["text"] != "plain prompt") {
t.Errorf("plain prompt changed: %s", raw)
}
if calls == 2 && (len(input) != 2 || input[1]["type"] != "mention") {
t.Errorf("file-only prompt changed: %s", raw)
}
return map[string]any{}
})
if err := chat.Prompt(context.Background(), fixtureThread, "plain prompt"); err != nil {
t.Fatal(err)
}
if err := chat.PromptWithMessageIDFiles(context.Background(), fixtureThread, "", "msg", []opencode.PromptFile{file}); err != nil {
t.Fatal(err)
}
}