fix(chat): settle authoritative turns and make capabilities explicit

This commit is contained in:
2026-10-20 15:45:09 +01:00
parent 016fcb552a
commit e504780976
16 changed files with 615 additions and 56 deletions
+62
View File
@@ -6,11 +6,13 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestPrepareChatAttachmentsWritesPrivateFileAndPart(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
data := base64.StdEncoding.EncodeToString([]byte("hello attachment"))
files, parts, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{
Name: "../../note.txt", Mime: "text/plain", Data: data,
@@ -44,7 +46,67 @@ func TestPrepareChatAttachmentsWritesPrivateFileAndPart(t *testing.T) {
}
}
func TestChatAttachmentStorageRetentionLeaseAndSymlink(t *testing.T) {
dir := filepath.Join(t.TempDir(), "uploads")
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
attachment := []protocol.ChatAttachment{{Name: "x", Data: base64.StdEncoding.EncodeToString([]byte("private"))}}
files, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
path := strings.TrimPrefix(files[0].URI, "file://")
old := time.Now().Add(-chatAttachmentRetention - time.Hour)
if err := os.Chtimes(path, old, old); err != nil {
t.Fatal(err)
}
more, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
defer releaseChatAttachments(more)
if _, err := os.Stat(path); err != nil {
t.Fatal("queued/active file collected", err)
}
releaseChatAttachments(files)
last, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
defer releaseChatAttachments(last)
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatal("expired unleased file not collected", err)
}
link := filepath.Join(t.TempDir(), "link")
if err := os.Symlink(dir, link); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", link)
if _, _, err := prepareChatAttachments("ses_test", attachment); err == nil {
t.Fatal("symlink storage accepted")
}
}
func TestChatAttachmentStorageQuota(t *testing.T) {
dir := t.TempDir()
if err := os.Chmod(dir, 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
file, err := os.Create(filepath.Join(dir, "quota"))
if err != nil {
t.Fatal(err)
}
if err := file.Truncate(chatAttachmentQuota); err != nil {
t.Fatal(err)
}
file.Close()
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "eA=="}}); err == nil || !strings.Contains(err.Error(), "quota") {
t.Fatalf("quota error = %v", err)
}
}
func TestPrepareChatAttachmentsRejectsInvalidAndOversized(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "not base64"}}); err == nil {
t.Fatal("invalid base64 accepted")
}