Skip to content
quarry Artifact [36bc0321f8]
Login

Artifact [36bc0321f8]

Artifact 36bc0321f80c5bdfc8ea0618547ab127c65bd55ad7079f4e7c8638c943918228:


package web

import (
	"context"
	"testing"

	"craftdesign.group/quarry/internal/ops"
)

type ogRepoIn struct {
	Repo string `json:"repo"`
}

type ogRepoOut struct {
	Private bool `json:"private"`
}

// A public repository's card may sit in any cache; a private one's stays
// in the reader's browser, and so does one whose visibility is unknown.
func TestOGCardCacheFollowsVisibility(t *testing.T) {
	r := ops.NewRegistry()
	if err := ops.Register(r, ops.Op[ogRepoIn, ogRepoOut]{
		Name: "repo.get", Caps: "r", Doc: "test stub", Kind: ops.Query, Auth: ops.Anonymous,
		Route: "GET /api/v1/r/{repo}",
		Run: func(_ context.Context, in ogRepoIn) (ogRepoOut, error) {
			switch in.Repo {
			case "open":
				return ogRepoOut{}, nil
			case "orchard":
				return ogRepoOut{Private: true}, nil
			}
			return ogRepoOut{}, ops.ErrNotFound
		},
	}); err != nil {
		t.Fatal(err)
	}
	for _, tc := range []struct{ repo, want string }{
		{"open", "public, max-age=3600"},
		{"orchard", "private, max-age=3600"},
		{"gone", "private, max-age=3600"},
	} {
		if got := ogCacheControl(context.Background(), r, tc.repo); got != tc.want {
			t.Errorf("FAIL: %s: %q, want %q", tc.repo, got, tc.want)
		}
	}
	t.Log("ok")
}