Some checks failed
CodeQL / Analyze (go) (push) Successful in 6m28s
Docker Image / build-docker (push) Failing after 13m26s
Lint and Testing / lint (push) Successful in 11m17s
Lint and Testing / test (push) Successful in 11m17s
Lint and Testing / golangci (push) Successful in 2m40s
128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestDiskCache_params_promote_list(t *testing.T) {
|
|
root := t.TempDir()
|
|
c, err := NewDiskCache(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
id := "319d72c7-301d-44fd-935f-3526dfb70f9f"
|
|
dir := c.waitingDir(id)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
params := TaskParams{
|
|
ID: id,
|
|
Created: "2026-03-31 21:37:46",
|
|
Status: string(statusReady),
|
|
Model: "ggml-small",
|
|
Text: "test",
|
|
}
|
|
if err := c.writeParams(dir, params); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
list, err := c.List()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if list[id]["status"] != string(statusReady) {
|
|
t.Fatalf("list: %v", list[id])
|
|
}
|
|
|
|
if err := c.PromoteToReady(id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, phase, err := c.LoadParams(id)
|
|
if err != nil || phase != cacheReady {
|
|
t.Fatalf("phase=%s err=%v", phase, err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(c.readyDir(id), fileParams)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestIsValidTaskID(t *testing.T) {
|
|
if !isValidTaskID("319d72c7-301d-44fd-935f-3526dfb70f9f") {
|
|
t.Fatal("uuid should be valid")
|
|
}
|
|
if isValidTaskID("../etc") {
|
|
t.Fatal("path traversal must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestResolveCacheRoot_absolute(t *testing.T) {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
abs, err := resolveCacheRoot("./cache")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := filepath.Join(cwd, "cache")
|
|
if abs != want {
|
|
t.Fatalf("got %q want %q", abs, want)
|
|
}
|
|
}
|
|
|
|
func TestDiskCache_RecoverInterrupted(t *testing.T) {
|
|
root := t.TempDir()
|
|
c, err := NewDiskCache(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id := "task-1"
|
|
dir := c.waitingDir(id)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := c.writeParams(dir, TaskParams{
|
|
ID: id, Created: "2026-01-01 00:00:00", Status: string(statusProcessing), Model: "m",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := c.RecoverInterrupted(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _, err := c.LoadParams(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Status != string(statusWaiting) {
|
|
t.Fatalf("got %q", p.Status)
|
|
}
|
|
}
|
|
|
|
func TestDiskCache_RecoverInterrupted_promotesReady(t *testing.T) {
|
|
root := t.TempDir()
|
|
c, err := NewDiskCache(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id := "done-task"
|
|
dir := c.waitingDir(id)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := c.writeParams(dir, TaskParams{
|
|
ID: id, Created: "2026-01-01 00:00:00", Status: string(statusReady), Model: "m", Text: "hi",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := c.RecoverInterrupted(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, phase, err := c.LoadParams(id)
|
|
if err != nil || phase != cacheReady {
|
|
t.Fatalf("phase=%s err=%v", phase, err)
|
|
}
|
|
}
|