Skip to content
quarry Artifact [159fb708ff]
Login

Artifact [159fb708ff]

Artifact 159fb708ff07fed8d2c7b25f8b75dffa33e45e07848741ec9c3db569cffdf9d9:


// Provenance gate (0002 ยง4 item 1): every third_party/ file and every
// salvage-headed file has a ledger row; every row points at a real file;
// GPL rows carry the explicit "accepted" marker.
package arch

import (
	"bufio"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

const (
	ledgerPath    = "../../third_party/PROVENANCE.md"
	salvageMarker = "Salvaged-from:"
)

type ledgerRow struct {
	path, source, commit, license, note string
}

func readLedger(t *testing.T) map[string]ledgerRow {
	t.Helper()
	raw, err := os.ReadFile(ledgerPath)
	if err != nil {
		t.Fatalf("ledger missing: %v", err)
	}
	rows := map[string]ledgerRow{}
	for _, line := range strings.Split(string(raw), "\n") {
		if !strings.HasPrefix(line, "|") || strings.HasPrefix(line, "| Path") || strings.HasPrefix(line, "|--") {
			continue
		}
		cells := strings.Split(strings.Trim(line, "|"), "|")
		if len(cells) == 1 && strings.TrimSpace(cells[0]) == "---" {
			continue
		}
		if len(cells) != 5 {
			// The separator row |---|---|... also lands here; skip dashes-only.
			dashesOnly := true
			for _, c := range cells {
				if strings.Trim(strings.TrimSpace(c), "-") != "" {
					dashesOnly = false
				}
			}
			if dashesOnly {
				continue
			}
			t.Fatalf("ledger row needs 5 cells: %q", line)
		}
		r := ledgerRow{
			path:    strings.TrimSpace(cells[0]),
			source:  strings.TrimSpace(cells[1]),
			commit:  strings.TrimSpace(cells[2]),
			license: strings.TrimSpace(cells[3]),
			note:    strings.TrimSpace(cells[4]),
		}
		if r.path == "" || r.source == "" || r.commit == "" || r.license == "" {
			t.Fatalf("ledger row incomplete: %q", line)
		}
		rows[r.path] = r
	}
	return rows
}

func TestProvenanceLedger(t *testing.T) {
	rows := readLedger(t)
	root := "../.."

	// Direction 1: every third_party file (bar the ledger itself) and
	// every salvage-headed file anywhere has a row.
	err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
		if err != nil {
			return err
		}
		rel, _ := filepath.Rel(root, path)
		if d.IsDir() {
			switch d.Name() {
			case ".zit-cache", "out", ".agent-config":
				return filepath.SkipDir
			}
			return nil
		}
		if rel == "third_party/PROVENANCE.md" || strings.HasPrefix(rel, ".zit") {
			return nil
		}
		inThirdParty := strings.HasPrefix(rel, "third_party/")
		headed := hasSalvageHeader(path)
		if (inThirdParty || headed) && rows[rel].path == "" {
			t.Errorf("%s: salvaged file with no ledger row", rel)
		}
		return nil
	})
	if err != nil {
		t.Fatal(err)
	}

	// Direction 2: no stale rows, and GPL rows carry the marker.
	for _, r := range rows {
		if _, err := os.Stat(filepath.Join(root, r.path)); err != nil {
			t.Errorf("ledger row %s: file does not exist", r.path)
		}
		if strings.Contains(strings.ToUpper(r.license), "GPL") &&
			!strings.Contains(strings.ToLower(r.note), "accepted") {
			t.Errorf("ledger row %s: GPL lineage without explicit 'accepted' note", r.path)
		}
	}
}

// hasSalvageHeader reports whether the file's first lines carry the
// salvage marker. Only the head is scanned: the marker is a header, and
// this file must not flag itself for containing the string in a constant.
func hasSalvageHeader(path string) bool {
	f, err := os.Open(path)
	if err != nil {
		return false
	}
	defer f.Close()
	sc := bufio.NewScanner(f)
	for i := 0; i < 5 && sc.Scan(); i++ {
		if strings.Contains(sc.Text(), salvageMarker) && !strings.Contains(sc.Text(), "salvageMarker") {
			return true
		}
	}
	return false
}