package calendar import ( "context" "errors" "strings" "testing" "github.com/google/uuid" ) func TestTaskBlockGuardBeforeSnapshotAccess(t *testing.T) { for _, mode := range []string{"external", "public", "shared", "other-owner"} { t.Run(mode, func(t *testing.T) { r, domain, actor, c := fixture(t) dir, _ := r.dir(domain, c.ID) // A deliberately absent snapshot detects ANY attempted refresh. c.Months["2026-09"] = strings.Repeat("a", 64) switch mode { case "external": c.Source = &Source{Kind: "ics", URL: "https://example.invalid/private.ics"} case "public": c.Public = true case "shared": c.Members = map[string]string{uuid.NewString(): "read"} case "other-owner": actor = "someone-else" } if err := saveMeta(dir, &c); err != nil { t.Fatal(err) } _, _, err := r.TaskBlock(context.Background(), domain, c.ID, actor, "fake", nil) if !errors.Is(err, ErrForbidden) && !errors.Is(err, ErrMissing) { t.Fatalf("snapshot accessed: %v", err) } called := false err = r.ReadPersonalLocalIndex(context.Background(), domain, c.ID, actor, func(Calendar) error { called = true; return nil }) if called || (!errors.Is(err, ErrForbidden) && !errors.Is(err, ErrMissing)) { t.Fatalf("range projection accessed blocked snapshot: %v", err) } var n int if err := r.DB.QueryRow(`SELECT count(*) FROM calendar_index_state`).Scan(&n); err != nil || n != 0 { t.Fatalf("cache populated %d %v", n, err) } }) } }