Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pkg/cli/audit_report_experiments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
func TestFindExperimentStatePath(t *testing.T) {
t.Parallel()
t.Run("returns empty when logsPath is empty", func(t *testing.T) {
t.Parallel()
assert.Empty(t, findExperimentStatePath(""), "should return empty string for empty logsPath")
})

t.Run("finds state.json at root", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
statePath := filepath.Join(dir, "state.json")
require.NoError(t, os.WriteFile(statePath, []byte("{}"), 0o600))
Expand All @@ -28,6 +30,7 @@ func TestFindExperimentStatePath(t *testing.T) {
})

t.Run("prefers state.jsonl at root", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "state.json"), []byte("{}"), 0o600))
statePath := filepath.Join(dir, "state.jsonl")
Expand All @@ -38,6 +41,7 @@ func TestFindExperimentStatePath(t *testing.T) {
})

t.Run("finds state.json in experiment subdirectory", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
subDir := filepath.Join(dir, "experiment")
require.NoError(t, os.MkdirAll(subDir, 0o755))
Expand All @@ -49,6 +53,7 @@ func TestFindExperimentStatePath(t *testing.T) {
})

t.Run("returns empty when no state.json exists", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
got := findExperimentStatePath(dir)
assert.Empty(t, got, "should return empty string when no state.json found")
Expand All @@ -58,27 +63,32 @@ func TestFindExperimentStatePath(t *testing.T) {
func TestExtractExperimentData(t *testing.T) {
t.Parallel()
t.Run("returns nil for empty logsPath", func(t *testing.T) {
t.Parallel()
assert.Nil(t, extractExperimentData(""), "should return nil for empty logsPath")
})

t.Run("returns nil when no state.json present", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
assert.Nil(t, extractExperimentData(dir), "should return nil when state.json missing")
})

t.Run("returns nil for invalid JSON", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "state.json"), []byte("not-json"), 0o600))
assert.Nil(t, extractExperimentData(dir), "should return nil for invalid JSON")
})

t.Run("returns nil for empty counts", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "state.json"), []byte(`{"counts":{}}`), 0o600))
assert.Nil(t, extractExperimentData(dir), "should return nil when counts map is empty")
})

t.Run("extracts single experiment with two variants", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
state := map[string]any{
"counts": map[string]any{
Expand All @@ -97,6 +107,7 @@ func TestExtractExperimentData(t *testing.T) {
})

t.Run("reads state.json from experiment subdirectory", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
subDir := filepath.Join(dir, "experiment")
require.NoError(t, os.MkdirAll(subDir, 0o755))
Expand All @@ -115,6 +126,7 @@ func TestExtractExperimentData(t *testing.T) {
})

t.Run("reads state.jsonl run ledger", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
raw := []byte(`{"run_id":"0","timestamp":"2026-07-31T23:00:00Z","assignments":{"style":"concise"}}
{"run_id":"1","timestamp":"2026-08-01T00:00:00Z","assignments":{"style":"concise"}}
Expand All @@ -129,6 +141,7 @@ func TestExtractExperimentData(t *testing.T) {
})

t.Run("extracts multiple experiments", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
state := map[string]any{
"counts": map[string]any{
Expand Down Expand Up @@ -184,6 +197,7 @@ func TestFormatExperimentLabel(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := formatExperimentLabel(tt.exp)
assert.Equal(t, tt.expected, got, "formatExperimentLabel result mismatch")
})
Expand Down Expand Up @@ -266,6 +280,7 @@ func TestExperimentMatchesFilter(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := experimentMatchesFilter(tt.exp, tt.experimentName, tt.variant)
assert.Equal(t, tt.want, got, "experimentMatchesFilter result mismatch")
})
Expand Down Expand Up @@ -306,6 +321,7 @@ func TestFormatExperimentSkipMessage(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := formatExperimentSkipMessage(tt.runID, tt.experiment, tt.variant)
assert.Contains(t, got, tt.wantSubstr, "formatExperimentSkipMessage output mismatch")
})
Expand Down Expand Up @@ -348,6 +364,7 @@ func TestDeriveLastSelectedVariant(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := deriveLastSelectedVariant(tt.counts)
assert.Equal(t, tt.expected, got, "deriveLastSelectedVariant result mismatch")
})
Expand All @@ -357,20 +374,23 @@ func TestDeriveLastSelectedVariant(t *testing.T) {
func TestFirstExperimentAssignment(t *testing.T) {
t.Parallel()
t.Run("returns false for nil", func(t *testing.T) {
t.Parallel()
name, variant, ok := firstExperimentAssignment(nil)
assert.False(t, ok)
assert.Empty(t, name)
assert.Empty(t, variant)
})

t.Run("returns false for empty assignments", func(t *testing.T) {
t.Parallel()
name, variant, ok := firstExperimentAssignment(&ExperimentData{Assignments: map[string]string{}})
assert.False(t, ok)
assert.Empty(t, name)
assert.Empty(t, variant)
})

t.Run("returns alphabetically first assignment", func(t *testing.T) {
t.Parallel()
exp := &ExperimentData{
Assignments: map[string]string{
"style": "concise",
Expand All @@ -387,6 +407,7 @@ func TestFirstExperimentAssignment(t *testing.T) {
func TestExtractExperimentDataWithRuns(t *testing.T) {
t.Parallel()
t.Run("uses last run record when runs array is present", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
state := map[string]any{
"counts": map[string]any{
Expand Down Expand Up @@ -417,6 +438,7 @@ func TestExtractExperimentDataWithRuns(t *testing.T) {
})

t.Run("falls back to heuristic when runs array is empty", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
state := map[string]any{
"counts": map[string]any{
Expand All @@ -435,6 +457,7 @@ func TestExtractExperimentDataWithRuns(t *testing.T) {
})

t.Run("falls back to heuristic when runs field is absent (legacy state)", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
state := map[string]any{
"counts": map[string]any{
Expand All @@ -451,6 +474,7 @@ func TestExtractExperimentDataWithRuns(t *testing.T) {
})

t.Run("skips last run record with empty assignments", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
state := map[string]any{
"counts": map[string]any{
Expand Down Expand Up @@ -478,6 +502,7 @@ func TestExtractExperimentDataWithRuns(t *testing.T) {
func TestExtractExperimentDataFallsBackToUsageSummary(t *testing.T) {
t.Parallel()
t.Run("reads assignments from usage activity summary when no state file present", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
usageDir := filepath.Join(dir, "usage", "activity")
require.NoError(t, os.MkdirAll(usageDir, 0o755))
Expand All @@ -500,6 +525,7 @@ func TestExtractExperimentDataFallsBackToUsageSummary(t *testing.T) {
})

t.Run("prefers state file over usage summary when both exist", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()

// Write experiment state file
Expand Down Expand Up @@ -532,6 +558,7 @@ func TestExtractExperimentDataFallsBackToUsageSummary(t *testing.T) {
})

t.Run("returns nil when usage summary has no experiments field", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
usageDir := filepath.Join(dir, "usage", "activity")
require.NoError(t, os.MkdirAll(usageDir, 0o755))
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/audit_report_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestParseDurationString(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := parseDurationString(tt.input)
if got != tt.expected {
t.Errorf("parseDurationString(%q) = %v, want %v", tt.input, got, tt.expected)
Expand Down Expand Up @@ -129,6 +130,7 @@ func TestTruncateString(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := stringutil.Truncate(tt.input, tt.maxLen)
if got != tt.expected {
t.Errorf("stringutil.Truncate(%q, %d) = %q, want %q", tt.input, tt.maxLen, got, tt.expected)
Expand Down Expand Up @@ -357,6 +359,7 @@ func TestAuditReportFileListingIntegration(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.path, func(t *testing.T) {
t.Parallel()
info, ok := fileMap[tc.path]
if !ok {
t.Fatalf("File %s not found in extracted files", tc.path)
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/awinfo_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestAwInfoStepsFieldParsing(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var info AwInfo
err := json.Unmarshal([]byte(tt.jsonContent), &info)
require.NoError(t, err, "Failed to unmarshal JSON")
Expand Down Expand Up @@ -137,6 +138,7 @@ func TestGetFirewallVersion(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.expected, tt.info.GetFirewallVersion(), tt.name)
})
}
Expand Down
Loading