GitRoot

craft your forge, build your project, grow your community freely
 1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: EUPL-1.2
 4
 5package main
 6
 7import (
 8	"bytes"
 9	"fmt"
10	"io/fs"
11	"testing"
12	"time"
13
14	gitroot "gitroot.dev/libs/golang/plugin"
15	"gitroot.dev/libs/golang/plugin/test"
16)
17
18func TestState(t *testing.T) {
19	// init mock
20	s := test.NewFakeServer(t, test.Override{
21		DiffWithParent: func(file, hash string) (string, error) {
22			return "```diff\n+ coucou\n```", nil
23		},
24	})
25	p := Build(s)
26
27	//init worktree
28	s.ModifyContent("index.md", "hello")
29
30	//var
31	branchName := "branchName"
32	graftFilepath := fmt.Sprintf("grafts/%s.md", branchName)
33	commit := gitroot.Commit{Branch: branchName, Hash: "0000", Message: "add branch", Date: time.Now()}
34
35	//check init
36	p.Init("repoName", false, `{"defaultTargetBranch": "main"}`)
37	p.StartCommit(commit)
38	p.AddFile("index.md")
39	p.EndCommit(commit)
40	p.Finish()
41	res, _ := fs.ReadFile(s.Worktree(), graftFilepath)
42	if !bytes.Contains(res, []byte("status: draft")) {
43		t.Errorf("---\n%s\n---\n\nwant\n\n---\n%s\n---", res, "status: draft")
44	}
45
46	//check start review
47	s.ModifyContent(graftFilepath, fmt.Sprintf("%s\n---\n/review\n", res))
48	p.Init("repoName", false, `{"defaultTargetBranch": "main"}`)
49	p.StartCommit(commit)
50	p.ModFile(graftFilepath, graftFilepath)
51	p.EndCommit(commit)
52	p.Finish()
53	res, _ = fs.ReadFile(s.Worktree(), graftFilepath)
54	if !bytes.Contains(res, []byte("status: review")) {
55		t.Errorf("---\n%s\n---\n\nwant\n\n---\n%s\n---", res, "status: review")
56	}
57
58	//check during review
59	s.ModifyContent("index.md", "hello\ncoucou")
60	p.Init("repoName", false, `{"defaultTargetBranch": "main"}`)
61	p.StartCommit(commit)
62	p.ModFile("index.md", "index.md")
63	p.EndCommit(commit)
64	p.Finish()
65	res, _ = fs.ReadFile(s.Worktree(), graftFilepath)
66	if !bytes.Contains(res, []byte("status: review")) {
67		t.Errorf("---\n%s\n---\n\nwant\n\n---\n%s\n---", res, "status: review")
68	}
69	if !bytes.Contains(res, []byte("```diff")) {
70		t.Errorf("---\n%s\n---\n\nwant\n\n---\n%s\n---", res, "```diff")
71	}
72
73	//check merge
74	s.ModifyContent(graftFilepath, fmt.Sprintf("%s\n---\n/merge\n", res))
75	p.Init("repoName", false, `{"defaultTargetBranch": "main"}`)
76	p.StartCommit(commit)
77	p.ModFile(graftFilepath, graftFilepath)
78	p.EndCommit(commit)
79	p.Finish()
80	res, _ = fs.ReadFile(s.Worktree(), graftFilepath)
81	if !bytes.Contains(res, []byte("status: done")) {
82		t.Errorf("---\n%s\n---\n\nwant\n\n---\n%s\n---", res, "status: review")
83	}
84	if s.NbCall.Merge != 1 {
85		t.Errorf("Merge called %d times", s.NbCall.Merge)
86	}
87}