xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / project / project_test.go

3.3 KB
1package project
2
3import (
4	"os"
5	"path/filepath"
6	"reflect"
7	"testing"
8)
9
10func names(ps []Project) map[string]int {
11	m := map[string]int{}
12	for _, p := range ps {
13		m[p.Name] = len(p.Sources)
14	}
15	return m
16}
17
18func TestGroup(t *testing.T) {
19	srcs := []Source{
20		{URL: "https://github.com/x/night-lights.git", Commits: 2, First: "2011-11-02", Roots: []string{"r1"}},
21		{URL: "https://gitlab.com/x/vim-night-lights.git", Commits: 5, First: "2011-11-02", Roots: []string{"r1"}},
22		{URL: "https://github.com/x/dotfiles.git", Commits: 80, First: "2014-10-13", Roots: []string{"r2"}},
23		{URL: "https://github.com/y/dotfiles.git", Commits: 101, First: "2026-01-28", Roots: []string{"r3"}},
24		{URL: "https://github.com/y/empty.git"},
25	}
26	ps, err := Group(srcs, Overrides{Rename: map[string]string{}})
27	if err != nil {
28		t.Fatal(err)
29	}
30	want := map[string]int{"vim-night-lights": 2, "dotfiles-2014": 1, "dotfiles": 1, "empty": 1}
31	if got := names(ps); !reflect.DeepEqual(got, want) {
32		t.Fatalf("projects = %v, want %v", got, want)
33	}
34
35	o := Overrides{Merge: [][2]string{{"https://github.com/x/dotfiles.git", "https://github.com/y/dotfiles.git"}}, Rename: map[string]string{"vim-night-lights": "night-lights"}}
36	ps, err = Group(srcs, o)
37	if err != nil {
38		t.Fatal(err)
39	}
40	want = map[string]int{"night-lights": 2, "dotfiles": 2, "empty": 1}
41	if got := names(ps); !reflect.DeepEqual(got, want) {
42		t.Fatalf("with overrides = %v, want %v", got, want)
43	}
44	if _, err := Group(srcs, Overrides{Merge: [][2]string{{"nope", "https://github.com/y/empty.git"}}}); err == nil {
45		t.Error("merge of an unknown URL should fail")
46	}
47}
48
49func TestReadOverrides(t *testing.T) {
50	path := filepath.Join(t.TempDir(), "overrides.txt")
51	os.WriteFile(path, []byte("# c\nmerge a b\nrename x y # why\n"), 0o644)
52	o, err := ReadOverrides(path)
53	if err != nil || len(o.Merge) != 1 || o.Rename["x"] != "y" {
54		t.Fatalf("o = %+v, %v", o, err)
55	}
56	os.WriteFile(path, []byte("hide\n"), 0o644)
57	if _, err := ReadOverrides(path); err == nil {
58		t.Error("bad rule should fail")
59	}
60	if _, err := ReadOverrides(filepath.Join(t.TempDir(), "missing")); err != nil {
61		t.Errorf("missing file: %v", err)
62	}
63}
64
65func TestRetag(t *testing.T) {
66	o := Overrides{Tag: map[string][]string{"p": {"+post-AI", "+big", "-backend", "+frontend", "+react", "-vim"}}}
67	got := o.retag("p", []string{"pre-AI", "small", "backend"}, known)
68	if want := []string{"post-AI", "big", "frontend"}; !reflect.DeepEqual(got, want) {
69		t.Fatalf("retag = %v, want %v", got, want)
70	}
71	if got := o.retag("p", []string{"vim", "cli"}, topicNames); !reflect.DeepEqual(got, []string{"cli", "react"}) {
72		t.Fatalf("retag topics = %v", got)
73	}
74	if validTags([]string{"-pre-AI"}) || validTags([]string{"+huge"}) || !validTags([]string{"-frontend", "+small", "-react"}) {
75		t.Error("validTags accepts the wrong rules")
76	}
77}
78
79// TestMatchTopics finds topics by dependency names and paths ignoring case, and by README words as written.
80func TestMatchTopics(t *testing.T) {
81	code := "go.mod\nweb/package.json\nDockerfile\n\nrequire github.com/gorilla/websocket v1\n{\"dependencies\": {\"react\": \"^19\", \"@types/bun\": \"1\"}}"
82	prose := "A WebRTC chat with a CLI. It does not use react, and nothing here is vim."
83	want := []string{"cli", "webrtc", "websocket", "react", "bun", "docker"}
84	if got := matchTopics(code, prose); !reflect.DeepEqual(got, want) {
85		t.Errorf("matchTopics = %v, want %v", got, want)
86	}
87}