|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + readinessExampleDirName = "testdata" |
| 12 | + readinessFollowSourceWatcherImport = "watcher_import" |
| 13 | + readinessSummaryAllPhasesPassed = "all readiness phases passed" |
| 14 | + readinessSummaryWarningPolicyFailed = "warning policy failed: WARN_FOLLOW_IMPORTS_HEALTH_STALE" |
| 15 | +) |
| 16 | + |
| 17 | +type readinessExampleFixture struct { |
| 18 | + Name string |
| 19 | + RelativePath string |
| 20 | + JSON bool |
| 21 | + Report readinessReport |
| 22 | +} |
| 23 | + |
| 24 | +func readinessExampleFixtures() []readinessExampleFixture { |
| 25 | + return []readinessExampleFixture{ |
| 26 | + { |
| 27 | + Name: "slow-ci-text", |
| 28 | + RelativePath: "example-slow-ci-success.txt", |
| 29 | + JSON: false, |
| 30 | + Report: exampleSlowCISuccessReport(), |
| 31 | + }, |
| 32 | + { |
| 33 | + Name: "ci-json", |
| 34 | + RelativePath: "example-ci-success.json", |
| 35 | + JSON: true, |
| 36 | + Report: exampleCISuccessReport(), |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: "release-warning-failure-text", |
| 40 | + RelativePath: "example-release-warning-failure.txt", |
| 41 | + JSON: false, |
| 42 | + Report: exampleReleaseWarningFailureReport(), |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func renderReadinessExample(report readinessReport, jsonOutput bool) ([]byte, error) { |
| 48 | + var buffer bytes.Buffer |
| 49 | + var err error |
| 50 | + if jsonOutput { |
| 51 | + err = writeReadinessJSON(&buffer, report) |
| 52 | + } else { |
| 53 | + err = writeReadinessSummary(&buffer, report) |
| 54 | + } |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + return buffer.Bytes(), nil |
| 59 | +} |
| 60 | + |
| 61 | +func writeReadinessExampleFixtures(baseDir string) ([]string, error) { |
| 62 | + fixtures := readinessExampleFixtures() |
| 63 | + writtenPaths := make([]string, 0, len(fixtures)) |
| 64 | + for _, fixture := range fixtures { |
| 65 | + body, err := renderReadinessExample(fixture.Report, fixture.JSON) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + path := filepath.Join(baseDir, fixture.RelativePath) |
| 70 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + if err := os.WriteFile(path, body, 0o644); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + writtenPaths = append(writtenPaths, path) |
| 77 | + } |
| 78 | + return writtenPaths, nil |
| 79 | +} |
| 80 | + |
| 81 | +func exampleCISuccessReport() readinessReport { |
| 82 | + startedAt := time.Date(2026, time.March, 17, 9, 0, 0, 0, time.UTC) |
| 83 | + completedAt := startedAt.Add(3 * time.Second) |
| 84 | + doctorStartedAt := startedAt |
| 85 | + doctorCompletedAt := doctorStartedAt.Add(400 * time.Millisecond) |
| 86 | + stdioCompletedAt := doctorCompletedAt.Add(600 * time.Millisecond) |
| 87 | + httpCompletedAt := stdioCompletedAt.Add(800 * time.Millisecond) |
| 88 | + |
| 89 | + report := readinessReport{ |
| 90 | + Status: readinessStatusOK, |
| 91 | + Summary: readinessSummaryAllPhasesPassed, |
| 92 | + KeepGoing: false, |
| 93 | + PolicyProfile: readinessPolicyProfileCI, |
| 94 | + SlowRunThresholdMS: 8000, |
| 95 | + SlowPhaseThresholdMS: 1000, |
| 96 | + StartedAt: &startedAt, |
| 97 | + CompletedAt: &completedAt, |
| 98 | + DurationMS: 3000, |
| 99 | + Doctor: exampleHealthyDoctorReport(), |
| 100 | + Stdio: readinessSmokeTest{ |
| 101 | + Status: readinessStatusOK, |
| 102 | + Summary: "mcp smoke test passed", |
| 103 | + }, |
| 104 | + HTTP: readinessSmokeTest{ |
| 105 | + Status: readinessStatusOK, |
| 106 | + Summary: "http mcp smoke test passed", |
| 107 | + }, |
| 108 | + Phases: []readinessPhaseResult{ |
| 109 | + {Name: readinessPhaseDoctor, Status: readinessStatusOK, Summary: "doctor --json passed", StartedAt: &doctorStartedAt, CompletedAt: &doctorCompletedAt, DurationMS: 400}, |
| 110 | + {Name: readinessPhaseStdio, Status: readinessStatusOK, Summary: "mcp smoke test passed", StartedAt: &doctorCompletedAt, CompletedAt: &stdioCompletedAt, DurationMS: 600}, |
| 111 | + {Name: readinessPhaseHTTP, Status: readinessStatusOK, Summary: "http mcp smoke test passed", StartedAt: &stdioCompletedAt, CompletedAt: &httpCompletedAt, DurationMS: 800}, |
| 112 | + }, |
| 113 | + } |
| 114 | + refreshWarningState(&report) |
| 115 | + return report |
| 116 | +} |
| 117 | + |
| 118 | +func exampleSlowCISuccessReport() readinessReport { |
| 119 | + report := exampleCISuccessReport() |
| 120 | + report.PolicyProfile = readinessPolicyProfileSlowCI |
| 121 | + report.SlowRunThresholdMS = 20000 |
| 122 | + report.SlowPhaseThresholdMS = 4000 |
| 123 | + refreshWarningState(&report) |
| 124 | + return report |
| 125 | +} |
| 126 | + |
| 127 | +func exampleReleaseWarningFailureReport() readinessReport { |
| 128 | + report := exampleCISuccessReport() |
| 129 | + report.Status = readinessStatusFailed |
| 130 | + report.PolicyProfile = readinessPolicyProfileRelease |
| 131 | + report.FailOnWarningCodes = []string{readinessWarnFollowHealthStale} |
| 132 | + report.Doctor = exampleHealthyDoctorReport() |
| 133 | + report.Doctor.Follow.HealthStale = true |
| 134 | + report.Doctor.Follow.Warnings = []doctorWarning{{Code: readinessWarnFollowHealthStale}} |
| 135 | + refreshWarningState(&report) |
| 136 | + report.Summary = readinessSummaryWarningPolicyFailed |
| 137 | + return report |
| 138 | +} |
| 139 | + |
| 140 | +func exampleHealthyDoctorReport() *doctorReport { |
| 141 | + updatedAt := time.Date(2026, time.March, 17, 8, 59, 55, 0, time.UTC) |
| 142 | + doctor := &doctorReport{ |
| 143 | + Status: "ok", |
| 144 | + } |
| 145 | + doctor.Runtime.ForeignKeys = true |
| 146 | + doctor.Runtime.RequiredSchemaOK = true |
| 147 | + doctor.Runtime.FTSReady = true |
| 148 | + doctor.Migrations.Pending = 0 |
| 149 | + doctor.Audit.NoteProvenanceReady = true |
| 150 | + doctor.Audit.ExclusionAuditReady = true |
| 151 | + doctor.Audit.ImportAuditReady = true |
| 152 | + doctor.Follow.HealthPresent = true |
| 153 | + doctor.Follow.LastUpdatedAt = &updatedAt |
| 154 | + doctor.Follow.Status = "ok" |
| 155 | + doctor.Follow.Source = readinessFollowSourceWatcherImport |
| 156 | + doctor.Follow.InputCount = 1 |
| 157 | + doctor.Follow.Continuous = true |
| 158 | + doctor.Follow.PollIntervalSeconds = 5 |
| 159 | + doctor.Follow.SnapshotAgeSeconds = 5 |
| 160 | + doctor.Follow.HealthStale = false |
| 161 | + doctor.Follow.RequestedWatchMode = "auto" |
| 162 | + doctor.Follow.ActiveWatchMode = "notify" |
| 163 | + doctor.MCP.Transport = readinessMCPTransportStdio |
| 164 | + doctor.MCP.ToolCount = 11 |
| 165 | + return doctor |
| 166 | +} |
0 commit comments