package main
import (
"errors"
"os"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/ops"
)
// A forge agent's rebuild starts only on a quiet forge and a store a
// rebuild of which is routine; past either, it is refused and starts
// nothing, and the refusal says which.
func TestAnAgentsRebuildWaitsForAQuietForgeAndASmallStore(t *testing.T) {
if err := upkeepRefusal("tcl", 0, 400<<20); err != nil {
t.Errorf("FAIL: a quiet forge and a 400 MiB store were refused: %v", err)
}
if err := upkeepRefusal("tcl", 1, 1<<20); !errors.Is(err, ops.ErrUnavailable) {
t.Errorf("FAIL: a rebuild under way did not hold the agent back: %v", err)
}
if err := upkeepRefusal("netbsd-src", 0, 3<<30); !errors.Is(err, ops.ErrForbidden) {
t.Errorf("FAIL: a 3 GiB store was rebuilt by an agent: %v", err)
}
}
// storeBytes reads a store file, or the .zit inside a directory.
func TestStoreBytesReadsTheStore(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, ".zit"), make([]byte, 1234), 0o644); err != nil {
t.Fatal(err)
}
if n := storeBytes(dir); n != 1234 {
t.Errorf("FAIL: a directory's store reads as %d bytes", n)
}
if n := storeBytes(filepath.Join(dir, ".zit")); n != 1234 {
t.Errorf("FAIL: a store file reads as %d bytes", n)
}
}