Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestFilesAndBatches(t *testing.T) {
|
|
checkFilesAndBatches(t, capacityStore(t))
|
|
}
|
|
|
|
func TestPostgresFilesAndBatches(t *testing.T) {
|
|
checkFilesAndBatches(t, openPG(t))
|
|
}
|
|
|
|
func checkFilesAndBatches(t *testing.T, db *DB) {
|
|
ctx := t.Context()
|
|
now := time.Now()
|
|
if err := db.PutFile(ctx, File{ID: "file-1", Owner: "p1", Purpose: "batch", Filename: "in.jsonl", CreatedAt: now},
|
|
[]byte("{\"a\":1}\n")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.PutFile(ctx, File{ID: "file-old", Owner: "p1", Purpose: "batch_output", Filename: "out.jsonl",
|
|
CreatedAt: now, ExpiresAt: now.Add(-time.Minute)}, []byte("x")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f, err := db.FileByID(ctx, "file-1")
|
|
if err != nil || f.Bytes != 8 || f.Owner != "p1" {
|
|
t.Fatalf("%+v %v", f, err)
|
|
}
|
|
if b, _ := db.FileContent(ctx, "file-1"); string(b) != "{\"a\":1}\n" {
|
|
t.Errorf("content %q", b)
|
|
}
|
|
if fs, _ := db.Files(ctx, "p1", "batch", 0); len(fs) != 1 {
|
|
t.Errorf("listed %v", fs)
|
|
}
|
|
if n, _ := db.DeleteExpiredFiles(ctx, now); n != 1 {
|
|
t.Errorf("expired %d", n)
|
|
}
|
|
|
|
b := Batch{ID: "batch-1", Owner: "p1", KeyID: "k", Endpoint: "/v1/chat/completions", InputFileID: "file-1",
|
|
CompletionWindow: "24h", Status: BatchValidating, CreatedAt: now, ExpiresAt: now.Add(24 * time.Hour)}
|
|
if err := db.PutBatch(ctx, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, ok, err := db.ClaimBatch(ctx, "gw-a", now, now.Add(time.Minute))
|
|
if err != nil || !ok || got.ID != "batch-1" {
|
|
t.Fatalf("claim: %+v %v %v", got, ok, err)
|
|
}
|
|
if _, ok, _ := db.ClaimBatch(ctx, "gw-b", now, now.Add(time.Minute)); ok {
|
|
t.Error("a leased batch was claimed twice")
|
|
}
|
|
if held, _ := db.RenewBatchLease(ctx, "batch-1", "gw-b", now.Add(time.Hour)); held {
|
|
t.Error("another gateway renewed the lease")
|
|
}
|
|
got.Status, got.Total, got.InProgressAt = BatchInProgress, 2, now
|
|
if err := db.UpdateBatch(ctx, got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, r := range []BatchResult{{Line: 1, OK: false, Data: "e"}, {Line: 0, OK: true, Data: "a"}, {Line: 0, OK: false, Data: "dup"}} {
|
|
if err := db.PutBatchResult(ctx, "batch-1", r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rs, _ := db.BatchResults(ctx, "batch-1")
|
|
if len(rs) != 2 || rs[0].Data != "a" || !rs[0].OK || rs[1].OK {
|
|
t.Errorf("results %+v", rs)
|
|
}
|
|
c, err := db.CancelBatch(ctx, "batch-1", now)
|
|
if err != nil || c.Status != BatchCancelling || c.Total != 2 || c.CancellingAt.IsZero() {
|
|
t.Errorf("cancel %+v %v", c, err)
|
|
}
|
|
if bs, _ := db.Batches(ctx, "p1", "", 0); len(bs) != 1 {
|
|
t.Errorf("listed %d", len(bs))
|
|
}
|
|
if _, err := db.BatchByID(ctx, "nope"); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("missing: %v", err)
|
|
}
|
|
}
|