Skip to content
quarry Artifact [1c24c7cca9]
Login

Artifact [1c24c7cca9]

Artifact 1c24c7cca951490297178de15eabc60c5c53f24e21a973e7ad7f5ed6db463717:


package artifact

import (
	"crypto/md5"
	"encoding/hex"
	"testing"
)

// withZ appends the §4.1 self-checksum card, which is what every
// artifact zit writes carries and what ordinary content never does.
func withZ(body string) []byte {
	sum := md5.Sum([]byte(body))
	return []byte(body + "Z " + hex.EncodeToString(sum[:]) + "\n")
}

// The case from the field: tcl's own CHANGELOG note begins "A NOTE ON
// THE CHANGELOG:", frames like a card stream and then fails to parse.
// It is content, and saying so 32 times per index buried 1,009 genuine
// failures it was mixed in with.
func TestOrdinaryProseThatBeginsLikeACardIsNotStructural(t *testing.T) {
	for _, text := range []string{
		"A NOTE ON THE CHANGELOG:\nthis file explains the format.\n",
		"A quick brown fox jumped over the lazy dog and kept going.\n",
		"C code that starts with a capital and a space, oddly.\n",
	} {
		if PlausiblyStructural([]byte(text)) {
			t.Errorf("FAIL: %q was called structural", text[:24])
		}
	}
}

// A real artifact that fails to parse still complains, because that is
// the failure worth reading.
func TestAFramedArtifactWithItsChecksumIsStructural(t *testing.T) {
	body := withZ("C a comment\nD 2026-09-10T10:00:00\n")
	if !PlausiblyStructural(body) {
		t.Error("FAIL: an artifact carrying a verified Z card was called content")
	}
}

// Bytes that never framed are never structural: no trailing newline, too
// short, a first line that is not a card.
func TestUnframedBytesAreNeverStructural(t *testing.T) {
	for name, raw := range map[string][]byte{
		"no trailing newline": []byte("C a comment"),
		"too short":           []byte("C x\n"),
		"lowercase first":     []byte("c a comment that is long enough\n"),
		"no space after":      []byte("Cxa comment that is long enough\n"),
		"empty":               {},
		"binary":              {0x00, 0x01, 0x02, '\n'},
	} {
		if PlausiblyStructural(raw) {
			t.Errorf("FAIL: %s was called structural", name)
		}
	}
}

// A Z card that does not check out means the bytes never framed at all,
// so it is not structural either — a corrupt artifact and a file that
// happens to end in a Z line are the same answer here: not ours.
func TestABadChecksumIsNotStructural(t *testing.T) {
	body := []byte("C a comment\nZ " + hex.EncodeToString(make([]byte, 16)) + "\n")
	if PlausiblyStructural(body) {
		t.Error("FAIL: a body whose checksum does not verify was called structural")
	}
}