package zitcli
import (
"context"
"strings"
"testing"
)
// AttachAdd writes the blob and the A card and answers both hashes in
// that order; AttachRm writes the sourceless card. Proved against a
// real store, because the two-hash stdout is zit 0.3.13's contract and
// nothing else checks it.
func TestAttachAddAndRmOnAStore(t *testing.T) {
z := New(testRepo(t, 1))
ctx := context.Background()
event, _, err := z.TechnoteAdd("dan", "", "text/x-markdown", "a note", "", []byte("body\n"))
if err != nil {
t.Fatalf("technote add: %v", err)
}
stub := []byte("quarry-attachment 1\nsha256 " + strings.Repeat("ab", 32) + "\nsize 12\ntype image/png\nname a.png\n")
art, blob, err := z.AttachAdd(ctx, event, "a.png", "image/png", "dan", "", stub)
if err != nil {
t.Fatalf("FAIL: attach add: %v", err)
}
if len(art) != 64 || len(blob) != 64 || art == blob {
t.Fatalf("FAIL: hashes = %q, %q", art, blob)
}
raw, err := z.ArtifactRaw(ctx, blob, 1<<16)
if err != nil || string(raw) != string(stub) {
t.Fatalf("FAIL: the blob is not the stub: %q, %v", raw, err)
}
card, err := z.ArtifactRaw(ctx, art, 1<<16)
if err != nil || !strings.Contains(string(card), "A a.png "+event+" "+blob) {
t.Fatalf("FAIL: the A card does not name the blob: %q, %v", card, err)
}
if !strings.Contains(string(card), "N image/png") {
t.Errorf("FAIL: the A card carries no N card: %q", card)
}
rm, err := z.AttachRm(ctx, event, "a.png", "dan")
if err != nil || len(rm) != 64 {
t.Fatalf("FAIL: attach rm: %q, %v", rm, err)
}
card, _ = z.ArtifactRaw(ctx, rm, 1<<16)
if !strings.Contains(string(card), "A a.png "+event+"\n") {
t.Errorf("FAIL: the removal card still names a source: %q", card)
}
t.Log("ok: attach add answers artifact then blob; rm writes the sourceless card")
}