package executor
import (
"testing"
millv1 "tangled.org/core/spindle/mill/proto/gen"
"tangled.org/core/spindle/models"
)
func TestCacheBindingsFromProtoUsesValidatedWorkflowEntries(t *testing.T) {
const (
repoDid = "did:web:example.com"
restoreID = "11111111-1111-4111-8111-111111111111"
saveID = "22222222-2222-4222-8222-222222222222"
)
entries := []models.CacheEntry{
{Key: "deps", Paths: []string{"node_modules"}, CompressionLevel: 7, When: "always"},
}
bindings, err := cacheBindingsFromProto(repoDid, entries, []*millv1.CacheBinding{{
EntryIndex: 0,
RestoreId: restoreID,
RestoreKey: "objects/" + repoDid + "/" + restoreID,
SaveId: saveID,
SaveKey: "objects/" + repoDid + "/" + saveID,
}})
if err != nil {
t.Fatal(err)
}
if len(bindings) != 1 {
t.Fatalf("bindings = %+v", bindings)
}
got := bindings[0]
if got.Key != "deps" || got.RestoreID != restoreID || got.SaveID != saveID || got.CompressionLevel != 7 || got.When != "always" {
t.Fatalf("binding = %+v", got)
}
}
func TestCacheBindingsFromProtoRejectsInvalidIndexes(t *testing.T) {
entries := []models.CacheEntry{{Key: "deps", Paths: []string{"node_modules"}}}
for _, bindings := range [][]*millv1.CacheBinding{
{{EntryIndex: 1}},
{{EntryIndex: 0}, {EntryIndex: 0}},
} {
if _, err := cacheBindingsFromProto("did:web:example.com", entries, bindings); err == nil {
t.Fatalf("cacheBindingsFromProto(%+v) succeeded", bindings)
}
}
}
func TestCacheBindingsFromProtoRejectsForeignAndUnpairedObjects(t *testing.T) {
const id = "11111111-1111-4111-8111-111111111111"
entries := []models.CacheEntry{{Key: "deps", Paths: []string{"node_modules"}}}
for _, binding := range []*millv1.CacheBinding{
{EntryIndex: 0, SaveId: id},
{EntryIndex: 0, SaveKey: "objects/did:web:example.com/" + id},
{EntryIndex: 0, RestoreId: id, RestoreKey: "objects/did:web:other.example/" + id},
} {
if _, err := cacheBindingsFromProto(
"did:web:example.com",
entries,
[]*millv1.CacheBinding{binding},
); err == nil {
t.Fatalf("foreign cache binding %+v succeeded", binding)
}
}
}