Skip to content
quarry Artifact [2d3ee86c19]
Login

Artifact [2d3ee86c19]

Artifact 2d3ee86c19be7a91dd1456b6fcf3562a4a7077e9462fd3e081485045cfe08ebc:


package api

import (
	"context"
	"errors"
	"io"
	"strings"
	"testing"

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

// config.export answers a configuration area as a download in the
// format config.import reads (4b8508f3): back up before you replace. A
// repository with no store says so rather than answering empty.
func TestAConfigurationAreaDownloadsForBackup(t *testing.T) {
	dir, _, _, _ := landedStore(t)
	r := ops.NewRegistry()
	if err := RegisterAll(r, Deps{Version: "v", Repos: NewRepos(map[string]*Repo{
		"alpha": {Zit: zitcli.New(dir)},
		"bare":  {},
	})}); err != nil {
		t.Fatal(err)
	}
	ctx := context.Background()
	out, err := r.Invoke(ctx, "config.export", configExportIn{Repo: "alpha", Area: "user"})
	if err != nil {
		t.Fatalf("FAIL: export: %v", err)
	}
	f, ok := out.(ops.File)
	if !ok {
		t.Fatalf("FAIL: answered %T, not a file", out)
	}
	rc, n, err := f.Open()
	if err != nil {
		t.Fatal(err)
	}
	body, _ := io.ReadAll(rc)
	rc.Close()
	if f.Name() != "alpha-user.txt" || !strings.HasPrefix(f.ContentType(), "text/plain") || f.Immutable() || f.ETag() != "" ||
		int64(len(body)) != n || !strings.HasPrefix(string(body), "#") {
		t.Fatalf("FAIL: %q %q immutable=%v etag=%q, %d/%d bytes: %.60q", f.Name(), f.ContentType(), f.Immutable(), f.ETag(), len(body), n, body)
	}
	if _, err := r.Invoke(ctx, "config.import", configImportIn{Repo: "alpha", File: string(body)}); err != nil {
		t.Fatalf("FAIL: the export does not import back: %v", err)
	}
	if _, err := r.Invoke(ctx, "config.export", configExportIn{Repo: "bare", Area: "shun"}); !errors.Is(err, ops.ErrUnavailable) {
		t.Fatalf("FAIL: a repository with no store answered %v, want unavailable", err)
	}
	t.Logf("ok: alpha-user.txt, %d bytes, imports back; a storeless repository is unavailable", n)
}