| 1 | package completion |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "reasonix/internal/evidence" |
| 6 | "reflect" |
| 7 | "runtime" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func ledgerOf(receipts ...evidence.Receipt) *evidence.Ledger { |
| 13 | l := evidence.NewLedger() |
| 14 | for _, r := range receipts { |
| 15 | l.Record(r) |
| 16 | } |
| 17 | return l |
| 18 | } |
| 19 | |
| 20 | func wrote(path string) evidence.Receipt { |
| 21 | return evidence.Receipt{ToolName: "write_file", Success: true, Write: true, Mutation: true, Paths: []string{path}} |
| 22 | } |
| 23 | |
| 24 | func read(path string) evidence.Receipt { |
| 25 | return evidence.Receipt{ToolName: "read_file", Success: true, Read: true, Paths: []string{path}, OutputBytes: 64} |
| 26 | } |
| 27 | |
| 28 | func ran(command string, ok bool) evidence.Receipt { |
| 29 | return evidence.Receipt{ToolName: "bash", Success: ok, Command: command, OutputBytes: 64} |
| 30 | } |
| 31 | |
| 32 | func gapKinds(rep Report) []string { return rep.GapKinds() } |
| 33 | |
| 34 | func TestFactsPreserveFailedAndStaleChecksWithoutQualityVerdict(t *testing.T) { |
| 35 | for _, success := range []bool{false, true} { |
| 36 | for _, laterWrite := range []bool{false, true} { |
| 37 | ledger := ledgerOf(wrote("parser.go"), ran("go test ./...", success)) |
| 38 | if laterWrite { |
| 39 | ledger.Record(wrote("auth/session.go")) |
| 40 | } |
| 41 | before := ledger.Receipts() |
| 42 | report := BuildFacts(ledger, "", nil) |
| 43 | if report.AssessmentKind != "facts" || report.Verdict != VerdictUnknown || len(report.Verifications) != 1 { |
| 44 | t.Fatalf("report=%+v", report) |
| 45 | } |
| 46 | check := report.Verifications[0] |
| 47 | if check.Passed != success || check.Stale != laterWrite { |
| 48 | t.Fatalf("check=%+v", check) |
| 49 | } |
| 50 | if len(report.Gaps) != 0 || !reflect.DeepEqual(before, ledger.Receipts()) { |
| 51 | t.Fatal("facts created inferred obligations or rewrote evidence") |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | func TestFactsDoNotInventMissingChecksOrReviews(t *testing.T) { |
| 57 | report := BuildFacts(ledgerOf(wrote("internal/auth/session.go"), wrote("schema/migration.sql")), "", nil) |
| 58 | if report.Verdict != VerdictUnknown || len(report.Verifications) != 0 || len(report.Gaps) != 0 || len(report.Changes) != 2 { |
| 59 | t.Fatalf("report=%+v", report) |
| 60 | } |
| 61 | } |
| 62 | func TestFactsExcludeScratchChanges(t *testing.T) { |
| 63 | project, scratch := t.TempDir(), t.TempDir() |
| 64 | path := filepath.Join(project, "main.go") |
| 65 | report := BuildFacts(ledgerOf(wrote(path), wrote(filepath.Join(scratch, "temp.go"))), project, []string{scratch}) |
| 66 | if runtime.GOOS == "windows" { |
| 67 | path = strings.ToLower(path) |
| 68 | } |
| 69 | if len(report.Changes) != 1 || report.Changes[0].Path != path { |
| 70 | t.Fatalf("changes=%+v", report.Changes) |
| 71 | } |
| 72 | } |
| 73 |