aboutsummaryrefslogtreecommitdiff
path: root/cmds/retrostar_test.go
blob: 3ad6dcaa8d720b688e6f43708d1b4af78e6dba7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Test for `fluxrec retrostar` against an httptest fake Miniflux: the
// printed candidates are model-ranked and anything already in labels.jsonl
// is excluded. Helpers (testEntry, makeEntries, pagedServer) come from
// export_test.go.
package cmds

import (
	"encoding/json"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"fluxrec/core"
)

func TestRetrostar(t *testing.T) {
	// pool has 20 read+unstarred candidates (IDs 100-119)
	fake := &pagedServer{read: makeEntries(20, 100, false, 1)}
	srv := httptest.NewServer(fake.handler())
	defer srv.Close()
	t.Setenv("MINIFLUX_URL", srv.URL)
	t.Setenv("MINIFLUX_TOKEN", "test-token")

	dir := t.TempDir()

	// Two candidates are already labeled and must be excluded.
	labelsPath := filepath.Join(dir, "labels.jsonl")
	now := time.Date(2024, 3, 1, 0, 0, 0, 0, time.UTC)
	if err := core.AppendLabels(labelsPath, []core.LabelRow{
		{EntryID: 105, URL: "https://example.com/post/105", Title: "Title 105",
			FeedID: 1, FeedTitle: "Fixture Feed", Label: core.LabelPos, CapturedAt: now},
		{EntryID: 110, URL: "https://example.com/post/110", Title: "Title 110",
			FeedID: 1, FeedTitle: "Fixture Feed", Label: core.LabelNeg, CapturedAt: now},
	}); err != nil {
		t.Fatalf("AppendLabels: %v", err)
	}

	modelPath := filepath.Join(dir, "model.json")
	body, _ := json.Marshal(testModel())
	if err := os.WriteFile(modelPath, body, 0o644); err != nil {
		t.Fatalf("writing model: %v", err)
	}

	cmd := &RetrostarCommand{ModelPath: modelPath, Labels: labelsPath, Days: 100000, Limit: 5}
	var out strings.Builder
	if err := cmd.Run(nil, &out); err != nil {
		t.Fatalf("retrostar: %v", err)
	}
	got := out.String()
	if !strings.Contains(got, "entry 100") {
		t.Errorf("output missing top candidate 100:\n%s", got)
	}
	for _, excluded := range []string{"entry 105", "entry 110"} {
		if strings.Contains(got, excluded) {
			t.Errorf("output contains already-labeled %s:\n%s", excluded, got)
		}
	}
	if !strings.Contains(got, "scanned 18 unlabeled candidates, showing 5") {
		t.Errorf("summary line wrong:\n%s", got)
	}
}