MCPcopy Create free account
hub / github.com/github/gh-aw / TestExtractExperimentData

Function TestExtractExperimentData

pkg/cli/audit_report_experiments_test.go:47–123  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

45}
46
47func TestExtractExperimentData(t *testing.T) {
48 t.Run("returns nil for empty logsPath", func(t *testing.T) {
49 assert.Nil(t, extractExperimentData(""), "should return nil for empty logsPath")
50 })
51
52 t.Run("returns nil when no state.json present", func(t *testing.T) {
53 dir := t.TempDir()
54 assert.Nil(t, extractExperimentData(dir), "should return nil when state.json missing")
55 })
56
57 t.Run("returns nil for invalid JSON", func(t *testing.T) {
58 dir := t.TempDir()
59 require.NoError(t, os.WriteFile(filepath.Join(dir, "state.json"), []byte("not-json"), 0o600))
60 assert.Nil(t, extractExperimentData(dir), "should return nil for invalid JSON")
61 })
62
63 t.Run("returns nil for empty counts", func(t *testing.T) {
64 dir := t.TempDir()
65 require.NoError(t, os.WriteFile(filepath.Join(dir, "state.json"), []byte(`{"counts":{}}`), 0o600))
66 assert.Nil(t, extractExperimentData(dir), "should return nil when counts map is empty")
67 })
68
69 t.Run("extracts single experiment with two variants", func(t *testing.T) {
70 dir := t.TempDir()
71 state := map[string]any{
72 "counts": map[string]any{
73 "caveman": map[string]int{"yes": 3, "no": 2},
74 },
75 }
76 raw, err := json.Marshal(state)
77 require.NoError(t, err)
78 require.NoError(t, os.WriteFile(filepath.Join(dir, "state.json"), raw, 0o600))
79
80 got := extractExperimentData(dir)
81 require.NotNil(t, got, "should return non-nil ExperimentData")
82 assert.Equal(t, "yes", got.Assignments["caveman"], "variant with highest count should be selected")
83 assert.Equal(t, 3, got.CumulativeCounts["caveman"]["yes"], "cumulative count for yes should be 3")
84 assert.Equal(t, 2, got.CumulativeCounts["caveman"]["no"], "cumulative count for no should be 2")
85 })
86
87 t.Run("reads state.json from experiment subdirectory", func(t *testing.T) {
88 dir := t.TempDir()
89 subDir := filepath.Join(dir, "experiment")
90 require.NoError(t, os.MkdirAll(subDir, 0o755))
91 state := map[string]any{
92 "counts": map[string]any{
93 "style": map[string]int{"concise": 1, "detailed": 2},
94 },
95 }
96 raw, err := json.Marshal(state)
97 require.NoError(t, err)
98 require.NoError(t, os.WriteFile(filepath.Join(subDir, "state.json"), raw, 0o600))
99
100 got := extractExperimentData(dir)
101 require.NotNil(t, got, "should return non-nil ExperimentData from subdir")
102 assert.Equal(t, "detailed", got.Assignments["style"], "detailed has higher count so should be selected")
103 })
104

Callers

nothing calls this directly

Calls 2

extractExperimentDataFunction · 0.85
RunMethod · 0.45

Tested by

no test coverage detected