Repository navigation
Expand file tree
/
Copy pathcell_report_test.go
More file actions
112 lines (102 loc) · 3.38 KB
/
Copy pathcell_report_test.go
File metadata and controls
112 lines (102 loc) · 3.38 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Agent-Field/codeaf/internal/cell"
"github.com/Agent-Field/codeaf/internal/cellstats"
"github.com/Agent-Field/codeaf/internal/cellsync"
"github.com/Agent-Field/codeaf/internal/home"
)
// reportCell makes a cell on a fresh home and records the given flushes for it.
func reportCell(t *testing.T, flushes ...cellsync.Flush) cell.Cell {
t.Helper()
t.Setenv("CODEAF_HOME", t.TempDir())
c, err := cell.Create(home.Dir(), cell.Options{Class: cell.Sandboxed})
if err != nil {
t.Fatal(err)
}
r := cellstats.NewRecorder(home.Dir(), c.ID, nil)
for _, f := range flushes {
r.OnFlush(f)
}
return c
}
func TestCellReport(t *testing.T) {
c := reportCell(t,
cellsync.Flush{Head: "aaaaaaaaaaaaaaaa", Turns: 2, Frames: 1, Objects: 14},
cellsync.Flush{Head: "bbbbbbbbbbbbbbbb", Turns: 1, Frames: 1, Objects: 3})
var out bytes.Buffer
if err := runCellIn([]string{"report", c.ID}, &out, t.TempDir()); err != nil {
t.Fatal(err)
}
for _, want := range []string{"aaaaaaaaaa ", "bbbbbbbbbb ", "total", " 17 "} {
if !strings.Contains(out.String(), want) {
t.Errorf("report lacks %q:\n%s", want, out.String())
}
}
}
func TestCellReportShowsTheVaultRow(t *testing.T) {
c := reportCell(t, cellsync.Flush{Head: "aaaaaaaaaaaaaaaa", Turns: 1})
cellstats.NewRecorder(home.Dir(), cellstats.VaultScope, cellstats.MeterFunc(func() cellstats.Counts {
return cellstats.Counts{Puts: 3, BytesUp: 4242}
})).Settle()
var out bytes.Buffer
if err := runCellIn([]string{"report", c.ID}, &out, t.TempDir()); err != nil {
t.Fatal(err)
}
if !strings.Contains(out.String(), "vault") || !strings.Contains(out.String(), "4242") {
t.Fatalf("report lacks the vault row:\n%s", out.String())
}
}
func TestCellReportPrintsNothingWithoutRows(t *testing.T) {
c := reportCell(t)
file := filepath.Join(t.TempDir(), "out.jsonl")
var out bytes.Buffer
if err := runCellIn([]string{"report", "--export", file, c.ID}, &out, t.TempDir()); err != nil {
t.Fatal(err)
}
if out.Len() != 0 {
t.Fatalf("an empty report printed %q", out.String())
}
if _, err := os.Stat(file); err == nil {
t.Fatal("an export of nothing wrote a file")
}
}
func TestCellReportExportOnlyOnRequest(t *testing.T) {
c := reportCell(t, cellsync.Flush{Head: "aaaa", Turns: 1, Objects: 2})
file := filepath.Join(t.TempDir(), "out.jsonl")
var plain bytes.Buffer
if err := runCellIn([]string{"report", c.ID}, &plain, t.TempDir()); err != nil {
t.Fatal(err)
}
if strings.Contains(plain.String(), cellstats.ExportNotice) {
t.Fatal("a plain report talked about exporting")
}
if _, err := os.Stat(file); err == nil {
t.Fatal("a plain report wrote an export")
}
var out bytes.Buffer
if err := runCellIn([]string{"report", c.ID, "--export", file}, &out, t.TempDir()); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
stats, _ := os.ReadFile(cellstats.Path(home.Dir(), c.ID))
if !bytes.Equal(got, stats) {
t.Fatalf("export = %q, want the stats lines %q", got, stats)
}
if !strings.Contains(out.String(), cellstats.ExportNotice) {
t.Fatalf("export did not say what it holds:\n%s", out.String())
}
}
func TestCellReportExportNeedsAFile(t *testing.T) {
c := reportCell(t)
if err := runCellIn([]string{"report", c.ID, "--export"}, &bytes.Buffer{}, t.TempDir()); err == nil {
t.Fatal("--export with no file was accepted")
}
}