"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetRelatedTestRunsToolDefinition = exports.getRelatedTestRunsResponseSchema = exports.currentRunContextSchema = exports.comparisonInsightsSchema = exports.testRunSummarySchema = exports.getRelatedTestRunsParamsSchema = void 0;
var zod_1 = require("zod");
var toolDefinition_js_1 = require("../toolDefinition.js");
var agentToolNames_js_1 = require("./agentToolNames.js");
/**
 * GetRelatedTestRunsDefinition - Tool definition for fetching and comparing test runs of the same test.
 */
exports.getRelatedTestRunsParamsSchema = zod_1.z.object({
    test_run_id: zod_1.z.string().optional().meta({
        description: 'Test run ID to find related runs for. Provide either test_run_id or test_id. ',
    }),
    test_id: zod_1.z
        .string()
        .optional()
        .meta({
        description: 'Test ID (invariant) to find related runs for. Provide either test_id or test_run_id. ' +
            'Use this when you have the test ID but not a specific test run ID (e.g. for locally-triggered runs).',
    }),
    filter_status: zod_1.z.enum(['passed', 'failed', 'all']).optional().meta({
        description: 'Filter runs by status. "passed" returns only passing runs, "failed" returns only failing runs, "all" returns all runs. Default: all',
    }),
    limit: zod_1.z.number().optional().meta({
        description: 'Maximum number of runs to return. Default: 10. Maximum: 50.',
    }),
    include_current: zod_1.z.boolean().optional().meta({
        description: 'Whether to include the current test run in results. Default: false',
    }),
    time_range: zod_1.z.enum(['recent', 'day', 'week', 'month']).optional().meta({
        description: 'Time range to filter runs. "recent" returns most recent runs regardless of time, "day" returns runs from last 24 hours, "week" from last 7 days, "month" from last 30 days. Default: recent',
    }),
    compare_same_browser: zod_1.z.boolean().optional().meta({
        description: 'If true, only returns runs that used the same browser as the current run. Useful for apples-to-apples comparison. Only applies to browser tests (ignored for API/mobile). Default: false',
    }),
    compare_same_environment: zod_1.z.boolean().optional().meta({
        description: 'If true, only returns runs that used the same environment as the current run. Useful for isolating environment-specific issues. Requires test_run_id. Default: true',
    }),
    compare_same_scenario: zod_1.z.boolean().optional().meta({
        description: 'If true, only returns runs that used the same scenario (data table row) as the current run. Useful for isolating scenario-specific failures. Requires test_run_id. Default: true',
    }),
    sort_by: zod_1.z.enum(['time', 'duration', 'status']).optional().meta({
        description: 'How to sort the results. "time" sorts by start time (newest first), "duration" sorts by execution duration, "status" groups by status. Default: time',
    }),
});
// Simplified test run summary - only essential fields
exports.testRunSummarySchema = zod_1.z.object({
    testRunId: zod_1.z.string(),
    testVariantId: zod_1.z.string().optional(),
    status: zod_1.z.string(),
    normalizedStatus: zod_1.z.enum(['passed', 'failed', 'other']),
    startTime: zod_1.z.string().optional(),
    durationMs: zod_1.z.number().optional(),
    durationFormatted: zod_1.z.string().optional(),
    browser: zod_1.z.string().optional(),
    planId: zod_1.z.string().optional(),
    environmentId: zod_1.z.string().optional(),
    scenarioId: zod_1.z.string().optional(),
    failureReason: zod_1.z.string().optional(),
    isCurrent: zod_1.z.boolean().optional(),
    isNewVersion: zod_1.z.boolean().optional(),
    relativeTime: zod_1.z.string().optional(),
});
// Simplified insights - only actionable data
exports.comparisonInsightsSchema = zod_1.z.object({
    totalRuns: zod_1.z.number(),
    passedRuns: zod_1.z.number(),
    failedRuns: zod_1.z.number(),
    passRate: zod_1.z.number(),
    lastPassedRun: exports.testRunSummarySchema.optional(),
    lastFailedRun: exports.testRunSummarySchema.optional(),
    lastPassedRunSameVersion: exports.testRunSummarySchema.optional(),
    currentVersionEverPassed: zod_1.z.boolean().optional(),
    avgDurationMs: zod_1.z.number().optional(),
    avgDurationFormatted: zod_1.z.string().optional(),
    isFlaky: zod_1.z.boolean(),
    flakinessNote: zod_1.z.string().optional(),
    consecutiveFailures: zod_1.z.number().optional(),
    consecutivePasses: zod_1.z.number().optional(),
    failureReasonBreakdown: zod_1.z
        .array(zod_1.z.object({
        reason: zod_1.z.string(),
        count: zod_1.z.number(),
        percentage: zod_1.z.number(),
    }))
        .optional(),
    browserBreakdown: zod_1.z
        .array(zod_1.z.object({
        browser: zod_1.z.string(),
        passRate: zod_1.z.number(),
        runCount: zod_1.z.number(),
    }))
        .optional()
        .meta({ description: 'Browser breakdown - only included for browser tests (not API/mobile tests)' }),
    versionBreakdown: zod_1.z
        .array(zod_1.z.object({
        variantId: zod_1.z.string(),
        passRate: zod_1.z.number(),
        runCount: zod_1.z.number(),
        firstSeen: zod_1.z.string().optional(),
        lastSeen: zod_1.z.string().optional(),
    }))
        .optional(),
    testWasModified: zod_1.z.boolean().optional(),
});
// Current run context includes a bit more detail
exports.currentRunContextSchema = zod_1.z.object({
    testRunId: zod_1.z.string(),
    testVariantId: zod_1.z.string().optional(),
    testName: zod_1.z.string().optional(),
    testType: zod_1.z.enum(['browser', 'api', 'mobile', 'performance', 'unknown']).optional().meta({
        description: 'The type of test (browser, api, mobile, etc.)',
    }),
    status: zod_1.z.string(),
    normalizedStatus: zod_1.z.enum(['passed', 'failed', 'other']),
    startTime: zod_1.z.string().optional(),
    durationMs: zod_1.z.number().optional(),
    durationFormatted: zod_1.z.string().optional(),
    browser: zod_1.z.string().optional().meta({ description: 'Browser used - only for browser tests' }),
    planId: zod_1.z.string().optional(),
    environmentId: zod_1.z.string().optional().meta({ description: 'Environment ID from the deployment parameters' }),
    scenarioId: zod_1.z.string().optional().meta({ description: 'Scenario ID (data table row) used in this run' }),
    failureReason: zod_1.z.string().optional(),
    failureSummary: zod_1.z.string().optional(),
});
exports.getRelatedTestRunsResponseSchema = zod_1.z.object({
    testId: zod_1.z.string(),
    currentRun: exports.currentRunContextSchema.optional(),
    previousRuns: zod_1.z.array(exports.testRunSummarySchema),
    insights: exports.comparisonInsightsSchema,
    note: zod_1.z.string().optional(),
});
exports.GetRelatedTestRunsToolDefinition = (0, toolDefinition_js_1.defineTool)({
    name: agentToolNames_js_1.AgentToolNames.GetRelatedTestRuns,
    description: "Retrieves and analyzes historical test runs of the same test for comparison and pattern detection.\n\nProvide either test_run_id or test_id to identify the test:\n- test_run_id: looks up the test run and derives the test identity; includes currentRun details in the response\n- test_id: uses the test ID (invariant) directly; no currentRun in the response\n\nUse this tool to:\n- Find the last passing run to compare against a failure\n- Detect if a test is flaky (alternating pass/fail)\n- Identify when a test started failing (regression detection)\n- Compare performance/timing across runs\n- Analyze patterns in test failures\n\nThe tool provides comparison insights including aggregate pass rates, flakiness detection,\nduration statistics, and breakdowns by failure reason, browser, and version. Each run summary includes **testRunId** for correlating with per-run tools (screenshots, artifacts, test run info).\n\n**Analyzing streaks and trends:** **consecutiveFailures** and aggregate stats are not a substitute for reading **failureReason** on **each** returned run (and **failureSummary** / **failureReason** on **currentRun** when present). Multiple consecutive failures can have **different** underlying causes \u2014 use **insights.failureReasonBreakdown** when present and widen **time_range** (week/month) and **limit** (up to max) so earlier runs with different errors are included before claiming a single root cause across history.",
    argumentsSchema: exports.getRelatedTestRunsParamsSchema,
    responseSchema: exports.getRelatedTestRunsResponseSchema,
});
//# sourceMappingURL=GetRelatedTestRunsDefinition.js.map