Skip to content
quarry Artifact [30ecd63ba8]
Login

Artifact [30ecd63ba8]

Artifact 30ecd63ba86ce780ba482f578caf02a0405e21d5586aeed9d0583af6c2304226:


package api

import (
	"path/filepath"
	"testing"

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

// A save that changes nothing writes nothing (quarry#377): saving a wiki
// page or a forum post with the text it already has answers with the
// version it has, and history gains no empty step.
func TestASaveThatChangesNothingWritesNothing(t *testing.T) {
	ix, z, _ := attachStore(t)
	db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { db.Close() })
	d := Deps{Version: "v", Repos: NewRepos(map[string]*Repo{"test": {Index: ix, Zit: z, Sync: zitSync(z, ix)}}),
		Removals: memRemovals{}, Forum: db}
	r := ops.NewRegistry()
	if err := RegisterAll(r, d); err != nil {
		t.Fatal(err)
	}
	dan := as("dan", "kf3")
	save := func(body string) string {
		t.Helper()
		out, err := r.Invoke(dan, "wiki.save", wikiSaveIn{Repo: "test", Name: "Notes", Body: body})
		if err != nil {
			t.Fatal(err)
		}
		return out.(wikiSaveOut).Hash
	}
	first := save("one\ntwo\n")
	if again := save("one\ntwo\n"); again != first {
		t.Errorf("FAIL: an unchanged wiki save wrote %.12s over %.12s", again, first)
	}
	if changed := save("one\ntwo\nthree\n"); changed == first {
		t.Error("FAIL: a changed wiki save wrote nothing")
	}

	out, err := r.Invoke(dan, "forum.post", forumPostIn{Repo: "test", Title: "hello", Body: "first\n"})
	if err != nil {
		t.Fatal(err)
	}
	root := out.(forumPostOut).Hash
	edit := func(title, body string) string {
		t.Helper()
		out, err := r.Invoke(dan, "forum.edit", forumEditIn{Repo: "test", Post: root, Title: title, Body: body})
		if err != nil {
			t.Fatal(err)
		}
		return out.(forumEditOut).Hash
	}
	if h := edit("hello", "first\n"); h != root {
		t.Errorf("FAIL: an unchanged forum edit wrote %.12s", h)
	}
	if h := edit("", "first\n"); h != root {
		t.Errorf("FAIL: an unchanged forum edit with no title wrote %.12s", h)
	}
	if h := edit("hello, renamed", "first\n"); h == root {
		t.Error("FAIL: a new title alone wrote nothing")
	}
}