86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLocalSessionCreationPreservesWinner(t *testing.T) {
|
||
|
|
target := filepath.Join(t.TempDir(), "session.yml")
|
||
|
|
start := make(chan struct{})
|
||
|
|
results := make(chan error, 2)
|
||
|
|
for _, content := range []string{"first", "second"} {
|
||
|
|
go func() { <-start; results <- writeLocalFile(target, []byte(content), false) }()
|
||
|
|
}
|
||
|
|
close(start)
|
||
|
|
first, second := <-results, <-results
|
||
|
|
if (first == nil) == (second == nil) {
|
||
|
|
t.Fatalf("want one winner: %v, %v", first, second)
|
||
|
|
}
|
||
|
|
failure := first
|
||
|
|
if failure == nil {
|
||
|
|
failure = second
|
||
|
|
}
|
||
|
|
if !errors.Is(failure, os.ErrExist) || !strings.Contains(failure.Error(), "--force") {
|
||
|
|
t.Fatalf("conflict = %v", failure)
|
||
|
|
}
|
||
|
|
data, err := os.ReadFile(target)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if string(data) != "first" && string(data) != "second" {
|
||
|
|
t.Fatalf("winner data = %q", data)
|
||
|
|
}
|
||
|
|
if err := writeLocalFile(target, []byte("replace"), true); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
data, err = os.ReadFile(target)
|
||
|
|
if err != nil || string(data) != "replace" {
|
||
|
|
t.Fatalf("forced replacement = %q, %v", data, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRemoteSessionCreationHonorsProviderCondition(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
pipeline, campaign, _ := writeValidConfigFiles(t, root)
|
||
|
|
fake := &storage.FakeBackend{}
|
||
|
|
key := artifacts.S3SessionConfigKey(artifacts.S3SessionPrefix("dnd", "sample-campaign", "2026-06-07"))
|
||
|
|
fake.UploadHook = func(call storage.FakeUploadCall) error {
|
||
|
|
fake.SeedObject(storage.FakeObject{Key: call.Key, Data: []byte("other writer")})
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
deps := fakeCommandDependencies(fake, nil)
|
||
|
|
args := []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--remote"}
|
||
|
|
var out, diagnostic bytes.Buffer
|
||
|
|
if status := executeWithDependencies(args, &out, &diagnostic, deps, executePlan); status == 0 || !strings.Contains(diagnostic.String(), "already exists") {
|
||
|
|
t.Fatalf("status=%d diagnostics=%q", status, diagnostic.String())
|
||
|
|
}
|
||
|
|
if got := string(fake.Objects[key].Data); got != "other writer" {
|
||
|
|
t.Fatalf("winner data = %q", got)
|
||
|
|
}
|
||
|
|
fake.UploadHook = nil
|
||
|
|
out.Reset()
|
||
|
|
diagnostic.Reset()
|
||
|
|
if status := executeWithDependencies(args, &out, &diagnostic, deps, executePlan); status == 0 || !strings.Contains(diagnostic.String(), "--force") {
|
||
|
|
t.Fatalf("existing status=%d diagnostics=%q", status, diagnostic.String())
|
||
|
|
}
|
||
|
|
if got := string(fake.Objects[key].Data); got != "other writer" {
|
||
|
|
t.Fatalf("existing data = %q", got)
|
||
|
|
}
|
||
|
|
out.Reset()
|
||
|
|
diagnostic.Reset()
|
||
|
|
if status := executeWithDependencies(append(args, "--force"), &out, &diagnostic, deps, executePlan); status != 0 {
|
||
|
|
t.Fatalf("forced status=%d diagnostics=%q", status, diagnostic.String())
|
||
|
|
}
|
||
|
|
if got := string(fake.Objects[key].Data); got == "other writer" || !strings.Contains(got, "2026-06-07") {
|
||
|
|
t.Fatalf("forced data = %q", got)
|
||
|
|
}
|
||
|
|
}
|