manus-tracker

tracker for scientific manuscripts
git clone git://src.adamsgaard.dk/manus-tracker # fast
git clone https://src.adamsgaard.dk/manus-tracker.git # slow
Log | Files | Refs | README Back to index

main_test.go (7527B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"net/http"
      6 	"net/http/httptest"
      7 	"os"
      8 	"os/exec"
      9 	"path/filepath"
     10 	"strings"
     11 	"testing"
     12 
     13 	"manus/bib"
     14 	"manus/store"
     15 )
     16 
     17 func git(t *testing.T, dir string, args ...string) string {
     18 	t.Helper()
     19 	out, err := exec.Command("git", append([]string{"-C", dir, "-c", "user.email=t@example.org", "-c", "user.name=t"}, args...)...).CombinedOutput()
     20 	if err != nil {
     21 		t.Fatalf("git %v: %v %s", args, err, out)
     22 	}
     23 	return string(out)
     24 }
     25 
     26 func newApp(t *testing.T) (*app, *bytes.Buffer) {
     27 	t.Helper()
     28 	if _, err := exec.LookPath("git"); err != nil {
     29 		t.Skip("git not available")
     30 	}
     31 	t.Setenv("MANUS_NOGIT", "1")
     32 	root := t.TempDir()
     33 	paper := filepath.Join(root, "paper")
     34 	os.MkdirAll(paper, 0o755)
     35 	os.WriteFile(filepath.Join(paper, "Makefile"), []byte("NAME=main\n"), 0o644)
     36 	os.WriteFile(filepath.Join(paper, "main.tex"), []byte("\\documentclass{article}\n\\title{Glacier Beds}\n\\cite{A2001}\n% TODO: shorten abstract\n\\bibliography{BIBnew}\n"), 0o644)
     37 	git(t, paper, "init", "-q")
     38 	git(t, paper, "add", ".")
     39 	git(t, paper, "commit", "-q", "-m", "init")
     40 	doc := filepath.Join(root, "coauthor")
     41 	os.MkdirAll(doc, 0o755)
     42 	os.WriteFile(filepath.Join(doc, "draft.docx"), []byte("x"), 0o644)
     43 	os.WriteFile(filepath.Join(root, "BIBnew.bib"), []byte("@article{A2001, author={Anne Berg}, title={Beds}, year={2001}, doi={10.5555/a}}\n"), 0o644)
     44 	var out bytes.Buffer
     45 	return &app{root: root, cwd: root, out: &out, bib: bib.NewClient()}, &out
     46 }
     47 
     48 func (a *app) must(t *testing.T, args ...string) string {
     49 	t.Helper()
     50 	buf := a.out.(*bytes.Buffer)
     51 	buf.Reset()
     52 	if err := a.run(args); err != nil {
     53 		t.Fatalf("manus %v: %v", args, err)
     54 	}
     55 	return buf.String()
     56 }
     57 
     58 func TestWorkflow(t *testing.T) {
     59 	a, _ := newApp(t)
     60 	a.must(t, "init", "paper", "coauthor")
     61 	s, _ := store.Load(a.root)
     62 	if m := s.MS["paper"]; m.Get("main") != "main.tex" || m.Get("repo") != "." || m.Get("title") != "Glacier Beds" {
     63 		t.Fatalf("paper = %v", m.Fields)
     64 	}
     65 	if m := s.MS["coauthor"]; m.Get("kind") != "docx" || m.Get("repo") != "" {
     66 		t.Fatalf("coauthor = %v", m.Fields)
     67 	}
     68 	if err := a.run([]string{"init", "paper"}); err == nil {
     69 		t.Fatal("duplicate init should fail")
     70 	}
     71 
     72 	a.cwd = filepath.Join(a.root, "paper")
     73 	if got := a.must(t, "add", "Redraw Fig. 1", "-d", "2026-12-01"); got != "paper#1\n" {
     74 		t.Fatalf("add = %q", got)
     75 	}
     76 	a.must(t, "add", "paper", "Check units")
     77 	a.must(t, "add", "coauthor", "Send comments")
     78 	a.must(t, "rel", "#1", "blocks", "#2")
     79 	a.must(t, "rel", "coauthor#1", "relates", "paper#1")
     80 	a.must(t, "mv", "#1", "doing")
     81 	a.must(t, "note", "#1", "colour", "scale", "too", "dark")
     82 	a.must(t, "set", "stage=submitted", "journal=J. Glaciol.")
     83 
     84 	got := a.must(t, "tasks")
     85 	if !strings.Contains(got, "paper#2  blocked*") || !strings.Contains(got, "paper#1  doing") {
     86 		t.Fatalf("tasks =\n%s", got)
     87 	}
     88 	a.cwd = a.root
     89 	if got := a.must(t, "tasks", "-s", "todo"); !strings.Contains(got, "coauthor#1") || strings.Contains(got, "paper#2") {
     90 		t.Fatalf("all todo tasks =\n%s", got)
     91 	}
     92 	if got := a.must(t, "show", "paper"); !strings.Contains(got, "colour scale too dark") || !strings.Contains(got, "blocked-by paper#1") || !strings.Contains(got, "relates coauthor#1") {
     93 		t.Fatalf("show =\n%s", got)
     94 	}
     95 	if got := a.must(t, "todos", "paper", "-a"); !strings.Contains(got, "imported 1 tasks") {
     96 		t.Fatalf("todos = %s", got)
     97 	}
     98 	if got := a.must(t, "todos", "paper", "-a"); strings.Contains(got, "imported") {
     99 		t.Fatalf("todos imported twice: %s", got)
    100 	}
    101 	if got := a.must(t, "ls"); !strings.Contains(got, "paper") || !strings.Contains(got, "submitted") {
    102 		t.Fatalf("ls = %s", got)
    103 	}
    104 
    105 	comments := filepath.Join(a.root, "review.txt")
    106 	os.WriteFile(comments, []byte("Reviewer 1\n\nLine 20 is unclear.\n\nFig 2 needs units.\n\nReviewer 2: The method\nis not novel.\n"), 0o644)
    107 	a.must(t, "revise", "paper", "-c", comments)
    108 	s, _ = store.Load(a.root)
    109 	r1 := s.MS["paper-r1"]
    110 	if r1 == nil || r1.Get("parent") != "paper" || r1.Get("tag") != "submitted-r0" || r1.Get("dir") != "paper-r1" || r1.Get("stage") != "revision" {
    111 		t.Fatalf("revision = %+v", r1)
    112 	}
    113 	if _, err := os.Stat(filepath.Join(a.root, "paper-r1", "main.tex")); err != nil {
    114 		t.Fatal("worktree missing")
    115 	}
    116 	if !strings.Contains(git(t, filepath.Join(a.root, "paper"), "tag"), "submitted-r0") {
    117 		t.Fatal("tag missing")
    118 	}
    119 	var titles []string
    120 	for _, tk := range r1.Tasks {
    121 		titles = append(titles, tk.Title)
    122 	}
    123 	want := []string{"Redraw Fig. 1", "Check units", "shorten abstract", "r1.1: Line 20 is unclear.", "r1.2: Fig 2 needs units.", "r2.1: The method is not novel."}
    124 	if strings.Join(titles, "|") != strings.Join(want, "|") {
    125 		t.Fatalf("revision tasks = %q", titles)
    126 	}
    127 	if r1.Task(1).Rels[0].To != (store.Ref{MS: "paper-r1", N: 2}) || r1.Task(1).Comments[0].Text != "colour scale too dark" {
    128 		t.Fatalf("carried task = %+v", r1.Task(1))
    129 	}
    130 	if p := s.MS["paper"].Task(1); p.Status != "done" || !strings.Contains(p.Comments[len(p.Comments)-1].Text, "paper-r1#1") {
    131 		t.Fatalf("parent task = %+v", p)
    132 	}
    133 	if got := a.must(t, "ls"); strings.Contains(got, "paper ") || !strings.Contains(got, "paper-r1") {
    134 		t.Fatalf("ls should hide superseded:\n%s", got)
    135 	}
    136 	if err := a.run([]string{"revise", "paper"}); err == nil {
    137 		t.Fatal("second revision of same parent should fail")
    138 	}
    139 
    140 	a.must(t, "revise", "coauthor")
    141 	if _, err := os.Stat(filepath.Join(a.root, "coauthor-r1", "draft.docx")); err != nil {
    142 		t.Fatal("copy missing")
    143 	}
    144 	a.cwd = filepath.Join(a.root, "paper-r1")
    145 	if got := a.must(t, "add", "Reply to editor"); got != "paper-r1#7\n" {
    146 		t.Fatalf("cwd detection in revision: %q", got)
    147 	}
    148 }
    149 
    150 func TestRefs(t *testing.T) {
    151 	a, _ := newApp(t)
    152 	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    153 		if r.URL.Path == "/10.5555/b" {
    154 			w.Write([]byte(`@article{Berg_2001, title={Other}, doi={10.5555/b}, author={Anne Berg}, journal={Journal of Glaciology}, year={2001}}`))
    155 			return
    156 		}
    157 		http.NotFound(w, r)
    158 	}))
    159 	defer srv.Close()
    160 	a.bib = &bib.Client{HTTP: srv.Client(), CrossrefURL: srv.URL + "/works", DOIURL: srv.URL}
    161 
    162 	if got := a.must(t, "ref", "add", "10.5555/A"); !strings.Contains(got, "already present as A2001") {
    163 		t.Fatalf("dup add = %q", got)
    164 	}
    165 	if got := a.must(t, "ref", "add", "https://doi.org/10.5555/b"); !strings.Contains(got, "@article{Berg2001,") || !strings.Contains(got, "J. Glaciol.") {
    166 		t.Fatalf("add = %q", got)
    167 	}
    168 	imp := filepath.Join(a.root, "lit.bib")
    169 	os.WriteFile(imp, []byte("@article{A2001, title={Different}, year={1999}}\n@article{X, doi={10.5555/b}}\n@book{C, title={C}}\n"), 0o644)
    170 	if got := a.must(t, "ref", "import", imp); !strings.Contains(got, "renamed A2001 -> A2001a") || !strings.Contains(got, "added 2, skipped 1") {
    171 		t.Fatalf("import = %q", got)
    172 	}
    173 	if got := a.must(t, "ref", "check"); !strings.Contains(got, "4 entries, 2 without doi") {
    174 		t.Fatalf("check = %q", got)
    175 	}
    176 	a.must(t, "init", "paper")
    177 	if got := a.must(t, "ref", "export", "paper", "-f", "ris"); !strings.Contains(got, "AU  - Berg, Anne") || strings.Contains(got, "Other") {
    178 		t.Fatalf("export = %q", got)
    179 	}
    180 	if got := a.must(t, "ref", "export", "-k", "C,Berg2001", "-f", "csl"); !strings.Contains(got, `"id": "Berg2001"`) || !strings.Contains(got, `"type": "book"`) {
    181 		t.Fatalf("export keys = %q", got)
    182 	}
    183 }
    184 
    185 func TestParseFlagsAnywhere(t *testing.T) {
    186 	a, _ := newApp(t)
    187 	if err := a.run([]string{"bogus"}); err == nil {
    188 		t.Fatal("unknown command should fail")
    189 	}
    190 	if err := a.run([]string{"add", "nope", "x", "-s", "weird"}); err == nil {
    191 		t.Fatal("bad status should fail")
    192 	}
    193 }