(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestBuildStatsEndToEnd(t *testing.T) { |
| 15 | root := t.TempDir() |
| 16 | claudeProj := t.TempDir() |
| 17 | |
| 18 | // flow.db with one done task that has a session. |
| 19 | db, err := flowdb.OpenDB(filepath.Join(root, "flow.db")) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | defer db.Close() |
| 24 | work := "/work/app" |
| 25 | mustExec(t, db, `INSERT INTO tasks (slug,name,status,work_dir,session_id,created_at,updated_at) |
| 26 | VALUES ('t1','T1','done',?, '00000000-0000-4000-8000-000000000001', '2026-06-01T00:00:00Z','2026-06-01T00:00:00Z')`, work) |
| 27 | |
| 28 | // Its jsonl under the encoded cwd path. |
| 29 | jdir := filepath.Join(claudeProj, claude.EncodeCwd(work)) |
| 30 | os.MkdirAll(jdir, 0o755) |
| 31 | line := `{"type":"assistant","timestamp":"2026-06-10T10:00:00.000Z","message":{"role":"assistant","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":0,"cache_read_input_tokens":0},"content":[{"type":"tool_use","name":"Bash","input":{"command":"flow show task"}}]}}` |
| 32 | os.WriteFile(filepath.Join(jdir, "00000000-0000-4000-8000-000000000001.jsonl"), []byte(line), 0o644) |
| 33 | |
| 34 | // auto-runs + owner + kb fixtures. |
| 35 | os.MkdirAll(filepath.Join(root, "tasks", "t1", "auto-runs"), 0o755) |
| 36 | os.WriteFile(filepath.Join(root, "tasks", "t1", "auto-runs", "r.log"), []byte("x"), 0o644) |
| 37 | os.MkdirAll(filepath.Join(root, "owners", "o1", "ticks"), 0o755) |
| 38 | os.WriteFile(filepath.Join(root, "owners", "o1", "ticks", "tick.log"), []byte("x"), 0o644) |
| 39 | os.MkdirAll(filepath.Join(root, "kb"), 0o755) |
| 40 | os.WriteFile(filepath.Join(root, "kb", "org.md"), []byte("# org\n- 2026-06-01 — fact one\n- 2026-06-02 — fact two\n"), 0o644) |
| 41 | |
| 42 | // Task t1 brief + updates — known sizes for ContextTokens assertion. |
| 43 | briefContent := []byte("# T1 brief\nThis is a test brief.\n") // 36 bytes |
| 44 | updatesContent := []byte("# update 1\nSome progress notes.\n") // 32 bytes |
| 45 | os.MkdirAll(filepath.Join(root, "tasks", "t1"), 0o755) |
| 46 | os.WriteFile(filepath.Join(root, "tasks", "t1", "brief.md"), briefContent, 0o644) |
| 47 | os.MkdirAll(filepath.Join(root, "tasks", "t1", "updates"), 0o755) |
| 48 | os.WriteFile(filepath.Join(root, "tasks", "t1", "updates", "u1.md"), updatesContent, 0o644) |
| 49 | |
| 50 | c := LoadCache(filepath.Join(root, "stats-cache.json")) |
| 51 | s, err := BuildStats(BuildOpts{ |
| 52 | Root: root, ClaudeProjects: claudeProj, DB: db, Cache: c, |
| 53 | Constants: DefaultConstants(), Since: time.Time{}, Project: "", |
| 54 | }) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if s.LookupsTotal != 1 || s.LookupsByKind[LookupResume] != 1 { |
| 59 | t.Errorf("lookups = %d %v, want 1 resume", s.LookupsTotal, s.LookupsByKind) |
| 60 | } |
| 61 | if s.Tokens.Total() != 15 { |
| 62 | t.Errorf("tokens = %d, want 15", s.Tokens.Total()) |
| 63 | } |
| 64 | if s.TasksDone != 1 || s.AutoRuns != 1 || s.OwnerTicks != 1 || s.KBFacts != 2 { |
| 65 | t.Errorf("counts done=%d auto=%d ticks=%d kb=%d", s.TasksDone, s.AutoRuns, s.OwnerTicks, s.KBFacts) |
| 66 | } |
| 67 | // ContextTokens: 1 resume lookup → briefBytes + updatesBytes, /4 for token approx. |
| 68 | wantContextTokens := (int64(len(briefContent)) + int64(len(updatesContent))) / 4 |
| 69 | if s.Savings.ContextTokens != wantContextTokens { |
| 70 | t.Errorf("ContextTokens = %d, want %d (brief=%d + updates=%d bytes / 4)", |
| 71 | s.Savings.ContextTokens, wantContextTokens, len(briefContent), len(updatesContent)) |
nothing calls this directly
no test coverage detected