package parser

import "testing"

func TestCodeParseFanInComplete(t *testing.T) {
	tests := []struct {
		name               string
		expected           int
		processed          int
		pathCount          int
		snapshotCodeFiles  int
		want               bool
	}{
		{
			// The scalar counter is NOT the completion authority: it can be
			// inflated above the true distinct count. A high counter with fewer
			// distinct paths must NOT be treated as complete.
			name:      "inflated counter but distinct paths not reached",
			expected:  952,
			processed: 952,
			pathCount: 480,
			want:      false,
		},
		{
			name:      "path count reached expected",
			expected:  952,
			processed: 900,
			pathCount: 952,
			want:      true,
		},
		{
			// The snapshot_files inventory count is NOT a completion authority:
			// its read can be momentarily partial and seal a truncated graph.
			// Completion requires distinct paths to reach the expected total.
			name:              "inventory accounted but distinct paths below expected",
			expected:          952,
			processed:         900,
			pathCount:         942,
			snapshotCodeFiles: 942,
			want:              false,
		},
		{
			name:              "still in flight",
			expected:          952,
			processed:         900,
			pathCount:         900,
			snapshotCodeFiles: 952,
			want:              false,
		},
		{
			name:      "zero expected",
			expected:  0,
			processed: 0,
			want:      true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := CodeParseFanInComplete(tt.expected, tt.processed, tt.pathCount, tt.snapshotCodeFiles)
			if got != tt.want {
				t.Fatalf("CodeParseFanInComplete() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestAccountedCodeFiles(t *testing.T) {
	if got := AccountedCodeFiles(900, 952); got != 952 {
		t.Fatalf("AccountedCodeFiles() = %d, want 952", got)
	}
	if got := AccountedCodeFiles(952, 900); got != 952 {
		t.Fatalf("AccountedCodeFiles() = %d, want 952", got)
	}
}

func TestCodeParseFailureRate(t *testing.T) {
	if got := CodeParseFailureRate(10, 952); got < 0.0104 || got > 0.0106 {
		t.Fatalf("CodeParseFailureRate() = %v, want ~0.0105", got)
	}
	if got := CodeParseFailureRate(0, 952); got != 0 {
		t.Fatalf("CodeParseFailureRate() = %v, want 0", got)
	}
}

func TestCodeParseStageStatus(t *testing.T) {
	if got := CodeParseStageStatus(10, 952, 0.05); got != "completed" {
		t.Fatalf("CodeParseStageStatus() = %q, want completed", got)
	}
	if got := CodeParseStageStatus(60, 952, 0.05); got != "failed" {
		t.Fatalf("CodeParseStageStatus() = %q, want failed", got)
	}
}
