Files
fil/docs/snippets/swift/plugins/plugin_testing.md
Henrik Jess Nielsen b4c07d3693
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s
Nomad changes
2026-06-01 23:40:55 +02:00

1.5 KiB

import Kreuzberg
import Testing

// Unit test a Swift Validator implementation
final class MinLengthValidator: Validator {
    let minLength: Int

    init(minLength: Int = 100) {
        self.minLength = minLength
    }

    func name() -> String { "test-validator" }
    func version() -> String { "1.0.0" }
    func priority() -> Int32 { 50 }
    func initialize() -> String { "{\"ok\": null}" }
    func shutdown() -> String { "{\"ok\": null}" }

    func validate(result: ExtractionResult, config: ExtractionConfig) -> String {
        let contentLength = result.content().count
        if contentLength < minLength {
            return "{\"err\": \"Content too short: \(contentLength) < \(minLength)\"}"
        }
        return "{\"ok\": null}"
    }

    func shouldValidate(result: ExtractionResult, config: ExtractionConfig) -> Bool {
        true
    }
}

// Unit test the validator by directly testing its logic.
// Integration tests exercise validators in-pipeline during extraction.

let validator = MinLengthValidator(minLength: 100)

// Create extraction config and result via the binding
let configJson = "{\"use_cache\": false}"
let config = try extractionConfigFromJson(configJson)

// Extract a document; the validator runs automatically during extraction
let result = try extractFile(path: "test.txt", mimeType: "text/plain", config: config)

// The validator's validate() method is invoked in-pipeline.
// If it rejects, the extraction throws an error.