Unify previous source resolution
This commit is contained in:
@@ -2,6 +2,8 @@ package previouscache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -58,6 +60,76 @@ func TestBuildPlanFallsBackToConfiguredOutputPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveUsesCommittedSourceMappingForCustomDestination(t *testing.T) {
|
||||
cfg, paths := previousCacheTestConfig(t)
|
||||
store := &storage.FakeBackend{}
|
||||
previous := previousManifestWithOutput(t, cfg, "artifacts/session_recap.md", nil)
|
||||
customKey := seedCommittedPreviousState(t, store, cfg, previous, []artifacts.RemoteArtifact{
|
||||
committedPublishedArtifactForTest("narratio.artifact.session_recap", "history/recap-v2.txt", []byte("# recap\n"), "recap-generation"),
|
||||
committedPublishedArtifactForTest("narratio.artifact.player_handout", "other/recap-v2.txt", []byte("handout"), "handout-generation"),
|
||||
})
|
||||
|
||||
plan, err := Resolve(context.Background(), cfg, paths, []artifacts.PreviousArtifactRequirement{{Name: "session_recap", Required: true}}, store)
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve() error = %v", err)
|
||||
}
|
||||
if len(plan.Records) != 2 {
|
||||
t.Fatalf("records = %#v, want manifest and artifact", plan.Records)
|
||||
}
|
||||
if manifestRecord := findRecord(plan.Records, InputKindManifest); manifestRecord == nil || len(manifestRecord.VerifiedContent) == 0 {
|
||||
t.Fatalf("manifest record = %#v, want retained verified content", manifestRecord)
|
||||
}
|
||||
artifact := findRecord(plan.Records, InputKindArtifact)
|
||||
if artifact == nil {
|
||||
t.Fatal("artifact record is missing")
|
||||
}
|
||||
if artifact.RemoteKey != customKey {
|
||||
t.Fatalf("remote key = %q, want exact configured source destination %q", artifact.RemoteKey, customKey)
|
||||
}
|
||||
if artifact.LocalRelativePath != "previous/artifacts/session_recap.md" {
|
||||
t.Fatalf("local relative path = %q, want source-local cache path", artifact.LocalRelativePath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDoesNotUseCommittedBasenameFallback(t *testing.T) {
|
||||
cfg, paths := previousCacheTestConfig(t)
|
||||
store := &storage.FakeBackend{}
|
||||
previous := previousManifestWithOutput(t, cfg, "artifacts/session_recap.md", nil)
|
||||
seedCommittedPreviousState(t, store, cfg, previous, []artifacts.RemoteArtifact{
|
||||
committedPublishedArtifactForTest("narratio.artifact.player_handout", "history/session_recap.md", []byte("wrong source"), "handout-generation"),
|
||||
})
|
||||
|
||||
_, err := Resolve(context.Background(), cfg, paths, []artifacts.PreviousArtifactRequirement{{Name: "session_recap", Required: true}}, store)
|
||||
if err == nil || !strings.Contains(err.Error(), `required previous-session artifact "session_recap" object missing`) {
|
||||
t.Fatalf("Resolve() error = %v, want source-mapping absence", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveRejectsAmbiguousLegacyCandidates(t *testing.T) {
|
||||
cfg, paths := previousCacheTestConfig(t)
|
||||
store := &storage.FakeBackend{}
|
||||
seedPreviousCurrent(t, store, cfg, previousManifestWithOutput(t, cfg, "artifacts/session_recap.md", []string{"archive/session_recap.md"}))
|
||||
prefix := artifacts.S3SessionPrefix("dnd", cfg.Session.Campaign, cfg.Session.PreviousSessionID)
|
||||
store.SeedObject(storage.FakeObject{Key: prefix + "artifacts/session_recap.md", Data: []byte("source candidate")})
|
||||
store.SeedObject(storage.FakeObject{Key: prefix + "archive/session_recap.md", Data: []byte("published candidate")})
|
||||
|
||||
_, err := Resolve(context.Background(), cfg, paths, []artifacts.PreviousArtifactRequirement{{Name: "session_recap", Required: true}}, store)
|
||||
if err == nil || !strings.Contains(err.Error(), "matches multiple published candidates") {
|
||||
t.Fatalf("Resolve() error = %v, want legacy ambiguity error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveHonorsCancellation(t *testing.T) {
|
||||
cfg, paths := previousCacheTestConfig(t)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
_, err := Resolve(ctx, cfg, paths, []artifacts.PreviousArtifactRequirement{{Name: "session_recap", Required: true}}, &storage.FakeBackend{})
|
||||
if err == nil || !strings.Contains(err.Error(), "context canceled") {
|
||||
t.Fatalf("Resolve() error = %v, want cancellation", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPlanSkipsMissingOptionalPreviousArtifact(t *testing.T) {
|
||||
cfg, paths := previousCacheTestConfig(t)
|
||||
store := &storage.FakeBackend{}
|
||||
@@ -238,3 +310,91 @@ func recordRelPaths(records []Record) []string {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func findRecord(records []Record, kind string) *Record {
|
||||
for index := range records {
|
||||
if records[index].Kind == kind {
|
||||
return &records[index]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedCommittedPreviousState(t *testing.T, store *storage.FakeBackend, cfg *config.Config, m *manifest.Manifest, published []artifacts.RemoteArtifact) string {
|
||||
t.Helper()
|
||||
prefix := artifacts.S3SessionPrefix(cfg.Pipeline.Storage.S3.RootPrefix, cfg.Session.Campaign, cfg.Session.PreviousSessionID)
|
||||
runID := "previous-run"
|
||||
manifestKey := artifacts.S3RunSessionManifestKey(prefix, runID)
|
||||
manifestData, err := marshalManifestForPreviousCacheTest(m)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal previous manifest: %v", err)
|
||||
}
|
||||
manifestGeneration := "manifest-generation"
|
||||
commit := artifacts.RemoteCommitManifest{
|
||||
FormatVersion: artifacts.RemoteCommitFormatVersion,
|
||||
Campaign: cfg.Session.Campaign,
|
||||
SessionID: cfg.Session.PreviousSessionID,
|
||||
RunID: runID,
|
||||
Artifacts: append([]artifacts.RemoteArtifact{{
|
||||
Type: artifacts.RemoteArtifactTypeSessionManifest,
|
||||
Source: "session.manifest",
|
||||
DestinationKey: manifestKey,
|
||||
SHA256: sha256ForPreviousCacheTest(manifestData),
|
||||
Size: int64(len(manifestData)),
|
||||
Generation: manifestGeneration,
|
||||
}}, published...),
|
||||
}
|
||||
commitData, err := artifacts.EncodeRemoteCommitManifest(commit)
|
||||
if err != nil {
|
||||
t.Fatalf("encode committed previous state: %v", err)
|
||||
}
|
||||
commitKey := artifacts.S3RunCommitKey(prefix, runID)
|
||||
commitGeneration := "commit-generation"
|
||||
pointerData, err := artifacts.EncodeCurrentCommitPointer(artifacts.CurrentCommitPointer{
|
||||
FormatVersion: artifacts.RemoteCommitFormatVersion,
|
||||
Campaign: cfg.Session.Campaign,
|
||||
SessionID: cfg.Session.PreviousSessionID,
|
||||
RunID: runID,
|
||||
CommitKey: commitKey,
|
||||
CommitSHA256: sha256ForPreviousCacheTest(commitData),
|
||||
CommitSize: int64(len(commitData)),
|
||||
CommitGeneration: commitGeneration,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("encode committed previous pointer: %v", err)
|
||||
}
|
||||
|
||||
store.SeedObject(storage.FakeObject{Key: artifacts.S3CurrentCommitPointerKey(prefix), Data: pointerData, ETag: "pointer-generation"})
|
||||
store.SeedObject(storage.FakeObject{Key: commitKey, Data: commitData, ETag: commitGeneration})
|
||||
store.SeedObject(storage.FakeObject{Key: manifestKey, Data: manifestData, ETag: manifestGeneration})
|
||||
for _, artifact := range published {
|
||||
store.SeedObject(storage.FakeObject{Key: artifact.DestinationKey, Data: publishedDataForTest(artifact.Source), ETag: artifact.Generation})
|
||||
}
|
||||
return published[0].DestinationKey
|
||||
}
|
||||
|
||||
func committedPublishedArtifactForTest(source, destination string, data []byte, generation string) artifacts.RemoteArtifact {
|
||||
return artifacts.RemoteArtifact{
|
||||
Type: artifacts.RemoteArtifactTypePublishedOutput,
|
||||
Source: source,
|
||||
DestinationKey: "dnd/campaigns/sample-campaign/sessions/2026-04-26/runs/previous-run/published/" + destination,
|
||||
SHA256: sha256ForPreviousCacheTest(data),
|
||||
Size: int64(len(data)),
|
||||
Generation: generation,
|
||||
}
|
||||
}
|
||||
|
||||
func publishedDataForTest(source string) []byte {
|
||||
if source == "narratio.artifact.session_recap" {
|
||||
return []byte("# recap\n")
|
||||
}
|
||||
if source == "narratio.artifact.player_handout" {
|
||||
return []byte("handout")
|
||||
}
|
||||
return []byte("published")
|
||||
}
|
||||
|
||||
func sha256ForPreviousCacheTest(data []byte) string {
|
||||
sum := sha256.Sum256(data)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user