/**
 * conformance.ts — shared conformance kit for driver authors + bundled drivers.
 *
 * `runConformanceKit({ manifest, driverModule })` returns a structured pass/fail
 * report. Used by the four bundled drivers' tests (ST07–ST09). Re-exposed
 * via the driver-sdk public surface for re-extraction readiness — see
 * driver-sdk/index.ts header.
 *
 * Checks:
 *  1. Every declared capability in manifest.capabilities[] has a matching
 *     function export on driverModule.
 *  2. Every function export returns a VerbResult-shaped object when invoked
 *     with a dummy arg (status, evidence_paths, duration_ms, retry_count).
 *  3. The manifest's transport field is structured (not a raw string).
 *  4. The manifest's trust.tier is one of the four allowed values.
 */
import { isVerbResult } from './idl.js';
const ALLOWED_TRUST_TIERS = new Set([
    'first_party_signed',
    'community_verified',
    'community_unverified',
    'local_unverified',
]);
function isStructuredTransport(t) {
    if (t === null || typeof t !== 'object' || Array.isArray(t))
        return false;
    const obj = t;
    if (typeof obj.mcp_server_id === 'string')
        return true;
    if (typeof obj.http_endpoint === 'string')
        return true;
    if (typeof obj.npm_package === 'string')
        return true;
    if (obj.builtin === true)
        return true;
    if (obj.local_command !== null &&
        typeof obj.local_command === 'object' &&
        !Array.isArray(obj.local_command)) {
        const lc = obj.local_command;
        return typeof lc.binary === 'string' && Array.isArray(lc.args);
    }
    return false;
}
/**
 * Run the conformance kit against a driver. Pass `dummyArgs` to control the
 * fixture invocation; default is an empty object (most drivers ignore args
 * in conformance mode and return a stub VerbResult).
 */
export async function runConformanceKit(opts) {
    const { manifest, driverModule } = opts;
    const dummyArgs = opts.dummyArgs ?? { target: { kind: 'css', value: '#stub' } };
    const checks = [];
    // Check 1: structured transport
    checks.push({
        id: 'transport-structured',
        description: 'manifest.transport must be a structured oneOf, never a raw string',
        status: isStructuredTransport(manifest.transport) ? 'pass' : 'fail',
        detail: isStructuredTransport(manifest.transport)
            ? undefined
            : `transport is not a structured variant: ${JSON.stringify(manifest.transport)}`,
    });
    // Check 2: trust tier is one of the four allowed
    checks.push({
        id: 'trust-tier-allowed',
        description: 'manifest.trust.tier must be one of the four allowed values',
        status: ALLOWED_TRUST_TIERS.has(manifest.trust?.tier) ? 'pass' : 'fail',
        detail: ALLOWED_TRUST_TIERS.has(manifest.trust?.tier)
            ? undefined
            : `trust.tier="${manifest.trust?.tier}" not in ${[...ALLOWED_TRUST_TIERS].join(', ')}`,
    });
    // Check 3: capabilities length >= 5
    checks.push({
        id: 'capabilities-min-five',
        description: 'manifest.capabilities must declare at least 5 IDL verbs',
        status: manifest.capabilities.length >= 5 ? 'pass' : 'fail',
        detail: manifest.capabilities.length >= 5
            ? undefined
            : `only ${manifest.capabilities.length} capabilities declared`,
    });
    // Check 4: every declared capability has a function export
    for (const cap of manifest.capabilities) {
        const fn = driverModule[cap];
        const ok = typeof fn === 'function';
        checks.push({
            id: `capability-export-${cap}`,
            description: `manifest.capabilities[${cap}] has a matching function export`,
            status: ok ? 'pass' : 'fail',
            detail: ok ? undefined : `driverModule.${cap} is not a function`,
        });
    }
    // Check 5: every declared capability returns a VerbResult shape
    for (const cap of manifest.capabilities) {
        const fn = driverModule[cap];
        if (typeof fn !== 'function')
            continue; // already failed Check 4
        try {
            const result = await fn(dummyArgs);
            const ok = isVerbResult(result);
            checks.push({
                id: `capability-shape-${cap}`,
                description: `driver.${cap}() returns a VerbResult shape`,
                status: ok ? 'pass' : 'fail',
                detail: ok ? undefined : `return shape: ${JSON.stringify(result)}`,
            });
        }
        catch (err) {
            checks.push({
                id: `capability-shape-${cap}`,
                description: `driver.${cap}() returns a VerbResult shape`,
                status: 'fail',
                detail: `threw: ${err instanceof Error ? err.message : String(err)}`,
            });
        }
    }
    const overall = checks.every((c) => c.status === 'pass') ? 'pass' : 'fail';
    return {
        driver_id: manifest.driverId,
        version: manifest.version,
        overall,
        checks,
    };
}
//# sourceMappingURL=conformance.js.map