File size: 9,411 Bytes
a10b189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
{-# LANGUAGE OverloadedStrings #-}

{-|
Module      : Assertica.Verify.CI
Description : CI integration for batch verification and reporting
Copyright   : (c) 2026 Ahmad Ali Proofs
License     : BSL-1.0

Provides functionality for CI systems to verify multiple files and generate
aggregated reports suitable for consumption by CI platforms.

Features:
- Batch verification of all .as files in a directory
- Aggregated JSON and CSV reporting
- Deterministic exit codes (0 = all pass, non-zero = any fail)
- Optional Haskell code generation for verified modules
- Timeout handling and robustness

DESIGN PRINCIPLES:
1. CI-friendly: Structured output, predictable exit codes
2. Deterministic: Same source tree produces identical report
3. Robust: Handles missing files, timeouts, I/O errors gracefully
4. Efficient: Process files in parallel where possible (future)
-}

module Assertica.Verify.CI
  ( -- * Main CI entry point
    ciVerify

    -- * Batch reporting
  , BatchVerificationReport (..)
  , generateBatchReport
  , generateBatchJSON
  , generateBatchCSV

    -- * Exit codes
  , getExitCode
  , ExitCode (..)

    -- * Code generation
  , generateHaskellForDirectory
  , generateHaskellForReport
  ) where

import Assertica.Verify.Pipeline (verifyDirectory)
import Assertica.Verify.Report
  ( VerificationReport (..)
  , toJSON
  , toHumanReadable
  , toCSV
  , csvHeader
  , Verdict (..)
  )
import Control.Monad (filterM)
import Data.List (sortBy)
import Data.Ord (comparing)
import System.FilePath (takeBaseName)
import System.Directory (doesDirectoryExist, listDirectory, createDirectoryIfMissing)
import System.Exit (ExitCode(..))

-- ============================================================================
-- BATCH VERIFICATION REPORT
-- ============================================================================

{-| Report aggregating results from multiple files.
-}
data BatchVerificationReport = BatchVerificationReport
  { bvrReports         :: [VerificationReport]  -- ^ Individual file reports
  , bvrTotalFiles      :: Int                    -- ^ Number of files verified
  , bvrSuccessful      :: Int                    -- ^ Number of successful verifications
  , bvrFailed          :: Int                    -- ^ Number of failed verifications
  , bvrTotalDuration   :: Double                 -- ^ Total time in milliseconds
  , bvrTimestamp       :: String                 -- ^ Report timestamp
  , bvrPassRate        :: Double                 -- ^ Success percentage
  }
  deriving (Show)

-- ============================================================================
-- CI VERIFICATION
-- ============================================================================

{-| Main entry point for CI verification.
Verifies all .as files in a directory and returns batch report.
-}
ciVerify :: FilePath -> IO BatchVerificationReport
ciVerify dirPath = do
  reports <- verifyDirectory dirPath
  generateBatchReport reports

{-| Generate batch report from individual reports.
-}
generateBatchReport :: [VerificationReport] -> IO BatchVerificationReport
generateBatchReport reports = do
  let total = length reports
      successful = length [r | r <- reports, vrVerdict r == Accept]
      failed = total - successful
      totalDuration = sum [vrTotalDuration r | r <- reports]
      passRate = if total == 0 then 0.0 else fromIntegral successful / fromIntegral total * 100.0
      timestamp = if null reports then "unknown" else vrTimestamp (head reports)

  return $ BatchVerificationReport
    { bvrReports = reports
    , bvrTotalFiles = total
    , bvrSuccessful = successful
    , bvrFailed = failed
    , bvrTotalDuration = totalDuration
    , bvrTimestamp = timestamp
    , bvrPassRate = passRate
    }

-- ============================================================================
-- EXIT CODES
-- ============================================================================

{-| Get exit code based on verification result.
Deterministic: Always same result for same input.
-}
getExitCode :: BatchVerificationReport -> ExitCode
getExitCode report
  | bvrFailed report == 0 = ExitSuccess
  | otherwise = ExitFailure (bvrFailed report)

-- ============================================================================
-- BATCH JSON REPORTING
-- ============================================================================

{-| Generate JSON report for batch verification.
Format:
{
  "timestamp": "...",
  "totalFiles": N,
  "successful": N,
  "failed": N,
  "passRate": X.X,
  "totalDuration": N.N,
  "files": [
    { ... full report JSON for each file ... }
  ]
}
-}
generateBatchJSON :: BatchVerificationReport -> String
generateBatchJSON report =
  "{\n"
  ++ "  \"timestamp\": \"" ++ bvrTimestamp report ++ "\",\n"
  ++ "  \"summary\": {\n"
  ++ "    \"totalFiles\": " ++ show (bvrTotalFiles report) ++ ",\n"
  ++ "    \"successful\": " ++ show (bvrSuccessful report) ++ ",\n"
  ++ "    \"failed\": " ++ show (bvrFailed report) ++ ",\n"
  ++ "    \"passRate\": " ++ show (bvrPassRate report) ++ ",\n"
  ++ "    \"totalDuration\": " ++ show (bvrTotalDuration report) ++ "\n"
  ++ "  },\n"
  ++ "  \"files\": [\n"
  ++ intercalateJSON (map toJSON (sortReports (bvrReports report)))
  ++ "\n  ]\n"
  ++ "}"

{-| Join JSON objects with commas.
-}
intercalateJSON :: [String] -> String
intercalateJSON [] = ""
intercalateJSON [x] = x
intercalateJSON (x:xs) = x ++ ",\n" ++ intercalateJSON xs

{-| Sort reports deterministically (by file path).
-}
sortReports :: [VerificationReport] -> [VerificationReport]
sortReports = sortBy (comparing vrFile)

-- ============================================================================
-- BATCH CSV REPORTING
-- ============================================================================

{-| Generate CSV report for batch verification.
Format: CSV with headers and one row per file.
-}
generateBatchCSV :: BatchVerificationReport -> String
generateBatchCSV report =
  unlines $
    [csvHeader] ++
    [toCSV r | r <- sortReports (bvrReports report)] ++
    ["# Summary"]  ++
    ["# Total: " ++ show (bvrTotalFiles report)]  ++
    ["# Successful: " ++ show (bvrSuccessful report)]  ++
    ["# Failed: " ++ show (bvrFailed report)]  ++
    ["# PassRate: " ++ show (bvrPassRate report) ++ "%"]

-- ============================================================================
-- HUMAN-READABLE BATCH REPORT
-- ============================================================================

{-| Generate human-readable batch report.
-}
generateBatchHumanReadable :: BatchVerificationReport -> String
generateBatchHumanReadable report =
  unlines
    [ "=========================================="
    , "BATCH VERIFICATION REPORT"
    , "=========================================="
    , ""
    , "Timestamp: " ++ bvrTimestamp report
    , "Total Files: " ++ show (bvrTotalFiles report)
    , "Successful: " ++ show (bvrSuccessful report)
    , "Failed: " ++ show (bvrFailed report)
    , "Pass Rate: " ++ show (bvrPassRate report) ++ "%"
    , "Total Duration: " ++ show (bvrTotalDuration report) ++ " ms"
    , ""
    , "=========================================="
    , "FILE RESULTS"
    , "=========================================="
    , ""
    ] ++
    [formatFileResult r | r <- sortReports (bvrReports report)]

{-| Format single file result.
-}
formatFileResult :: VerificationReport -> String
formatFileResult vr =
  let status = case vrVerdict vr of
                 Accept -> "PASS"
                 Reject -> "FAIL"
      marker = if status == "PASS" then "✓" else "✗"
  in marker ++ " " ++ vrFile vr ++ " [" ++ status ++ ", " ++ show (vrTotalDuration vr) ++ "ms]"

-- ============================================================================
-- CODE GENERATION FOR VERIFIED MODULES
-- ============================================================================

{-| Generate Haskell code for all successfully verified modules in a directory.
-}
generateHaskellForDirectory :: FilePath -> FilePath -> IO ()
generateHaskellForDirectory sourceDir outputDir = do
  reports <- verifyDirectory sourceDir
  generateHaskellForReport reports outputDir

{-| Generate Haskell code for verified modules from reports.
For each successfully verified file, generates corresponding Haskell code.
-}
generateHaskellForReport :: [VerificationReport] -> FilePath -> IO ()
generateHaskellForReport reports outputDir = do
  createDirectoryIfMissing True outputDir

  -- Filter to successful verifications
  let successful = [r | r <- reports, vrVerdict r == Accept]

  -- Generate Haskell for each
  mapM_ (generateHaskellForFile outputDir) successful

{-| Generate Haskell code for a single verified module.
-}
generateHaskellForFile :: FilePath -> VerificationReport -> IO ()
generateHaskellForFile outputDir vr = do
  -- Placeholder: real implementation would extract code from verification
  -- For now, we just create a stub
  let outputFile = outputDir ++ "/" ++ takeBaseName (vrFile vr) ++ ".hs"
  writeFile outputFile $ generateHaskellStub vr

{-| Generate Haskell stub for a module.
Placeholder implementation.
-}
generateHaskellStub :: VerificationReport -> String
generateHaskellStub vr =
  "-- Generated from " ++ vrFile vr ++ "\n" ++
  "-- Verified at " ++ vrTimestamp vr ++ "\n" ++
  "-- Status: " ++ show (vrVerdict vr) ++ "\n" ++
  "\n" ++
  "module Generated where\n" ++
  "\n" ++
  "-- Generated code placeholder\n"