package engine
import (
"context"
"errors"
"io"
"log/slog"
"os"
"testing"
"tangled.org/core/spindle/artifactstore"
"tangled.org/core/spindle/models"
)
type fakeLogStores []artifactstore.PutResult
func (f fakeLogStores) PutFile(context.Context, string, string) []artifactstore.PutResult { return f }
func TestLocalLogStaysWhenOnlyOneStoreTookTheArchive(t *testing.T) {
d := newTestDB(t)
t.Cleanup(func() { d.Close() })
logDir := t.TempDir()
wid := models.WorkflowId{PipelineId: "partial", Name: "build"}
path := models.LogFilePath(logDir, wid)
if err := os.WriteFile(path, []byte("log line\n"), 0o644); err != nil {
t.Fatal(err)
}
l := slog.New(slog.NewTextHandler(io.Discard, nil))
archiveLogTo(l, fakeLogStores{{}, {Err: errors.New("s3: unreachable")}}, d, logDir, wid, "did:plc:repo")
if _, err := d.GetFinishedLog(wid); err != nil {
t.Fatalf("no archive row although one store took the log: %v", err)
}
if _, err := os.Stat(path); err != nil {
t.Fatal("the local log was removed although one of the stores failed")
}
}