{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://the-ai-platform.dev/schemas/tap.test.schema.json",
  "title": "The AI Platform miniapp test descriptor",
  "description": "Strict schema-v2 descriptor for deterministic miniapp test profiles and matrix entries. Required origins, effects, credentials, and capabilities are preflight requirements only and never grant host authority.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "packageId",
    "configPath",
    "profiles",
    "matrix"
  ],
  "properties": {
    "$schema": {
      "type": "string",
      "const": "https://the-ai-platform.dev/schemas/tap.test.schema.json"
    },
    "schemaVersion": {
      "type": "integer",
      "const": 2
    },
    "packageId": {
      "$ref": "#/$defs/packageId"
    },
    "configPath": {
      "$ref": "#/$defs/confinedPath"
    },
    "testMatch": {
      "$ref": "#/$defs/testMatch"
    },
    "profiles": {
      "type": "object",
      "minProperties": 1,
      "maxProperties": 64,
      "propertyNames": {
        "$ref": "#/$defs/id"
      },
      "additionalProperties": {
        "$ref": "#/$defs/profile"
      }
    },
    "matrix": {
      "type": "array",
      "minItems": 1,
      "maxItems": 256,
      "items": {
        "$ref": "#/$defs/matrixEntry"
      }
    },
    "waivers": {
      "type": "array",
      "maxItems": 128,
      "items": {
        "$ref": "#/$defs/waiver"
      }
    }
  },
  "$defs": {
    "trimmed": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "^\\S(?:.*\\S)?$"
    },
    "id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[a-z0-9][a-z0-9._:-]*$"
    },
    "contributionId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "^[A-Za-z0-9_-]+$"
    },
    "actionId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^(?:\\*|do:\\*|[a-z0-9][a-z0-9._:-]*)$"
    },
    "permissionScenario": {
      "description": "A canonical descriptor-owned permission scenario. Host-internal synthetic scenarios are not accepted.",
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^(?:default|all-denied|read-only|http-denied|deny:[a-z0-9][a-z0-9._:-]*)$"
    },
    "capabilityId": {
      "type": "string",
      "minLength": 8,
      "maxLength": 2048,
      "pattern": "^(?:action:[a-z0-9][a-z0-9._:-]*|effect:[a-z0-9][a-z0-9._-]*:\\S+)$"
    },
    "effectToken": {
      "type": "string",
      "minLength": 10,
      "maxLength": 2048,
      "pattern": "^effect:[a-z0-9][a-z0-9._-]*:\\S+$"
    },
    "packageId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "^[A-Za-z0-9@][A-Za-z0-9@/._:-]*$"
    },
    "confinedPath": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1024,
      "pattern": "^(?!/)(?![A-Za-z]:)(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\)(?!.*//)(?!.*(?:^|/)\\.(?:/|$)).+$"
    },
    "confinedGlob": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1024,
      "pattern": "^(?!/)(?![A-Za-z]:)(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\)(?!.*//)(?!.*(?:^|/)\\.(?:/|$)).+$"
    },
    "testMatch": {
      "type": "array",
      "minItems": 1,
      "maxItems": 128,
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/confinedGlob"
      }
    },
    "stringIdSet": {
      "type": "array",
      "maxItems": 128,
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/id"
      }
    },
    "fileSet": {
      "type": "array",
      "maxItems": 64,
      "uniqueItems": true,
      "items": {
        "allOf": [
          {
            "$ref": "#/$defs/confinedPath"
          },
          {
            "type": "string",
            "pattern": "\\.json$"
          }
        ]
      }
    },
    "originSet": {
      "type": "array",
      "maxItems": 128,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 8,
        "maxLength": 2048,
        "pattern": "^https?://[^/?#]+$"
      }
    },
    "viewport": {
      "type": "object",
      "additionalProperties": false,
      "required": ["width", "height"],
      "properties": {
        "width": {
          "type": "integer",
          "minimum": 320,
          "maximum": 8192
        },
        "height": {
          "type": "integer",
          "minimum": 200,
          "maximum": 8192
        }
      }
    },
    "environment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "viewport",
        "locale",
        "timezone",
        "theme",
        "reducedMotion",
        "seed"
      ],
      "properties": {
        "viewport": {
          "$ref": "#/$defs/viewport"
        },
        "locale": {
          "type": "string",
          "minLength": 2,
          "maxLength": 64,
          "pattern": "^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$"
        },
        "timezone": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "theme": {
          "enum": ["light", "dark"]
        },
        "reducedMotion": {
          "type": "boolean"
        },
        "seed": {
          "type": "integer",
          "minimum": 0,
          "maximum": 4294967295
        },
        "fixedNow": {
          "type": "string",
          "minLength": 20,
          "maxLength": 64,
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d{1,9})?Z$"
        }
      }
    },
    "capturePolicy": {
      "enum": ["off", "failure-only", "always"]
    },
    "artifacts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["trace", "screenshots"],
      "properties": {
        "trace": {
          "$ref": "#/$defs/capturePolicy"
        },
        "screenshots": {
          "$ref": "#/$defs/capturePolicy"
        },
        "maxBytes": {
          "type": "integer",
          "minimum": 1024,
          "maximum": 67108864
        }
      }
    },
    "httpCredentialMetadataText": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "^[^\\s\\u0000-\\u001F\\u007F-\\u009F](?:[^\\u0000-\\u001F\\u007F-\\u009F]*[^\\s\\u0000-\\u001F\\u007F-\\u009F])?$"
    },
    "httpCredentialFixture": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "slot",
            "credentialType",
            "displayName",
            "metadataFields"
          ],
          "properties": {
            "slot": { "$ref": "#/$defs/id" },
            "credentialType": { "const": "http_bearer" },
            "displayName": {
              "$ref": "#/$defs/httpCredentialMetadataText"
            },
            "metadataFields": {
              "type": "object",
              "additionalProperties": false,
              "maxProperties": 0
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "slot",
            "credentialType",
            "displayName",
            "metadataFields"
          ],
          "properties": {
            "slot": { "$ref": "#/$defs/id" },
            "credentialType": { "const": "http_basic" },
            "displayName": {
              "$ref": "#/$defs/httpCredentialMetadataText"
            },
            "metadataFields": {
              "type": "object",
              "additionalProperties": false,
              "required": ["username"],
              "properties": {
                "username": {
                  "$ref": "#/$defs/httpCredentialMetadataText"
                }
              }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "slot",
            "credentialType",
            "displayName",
            "metadataFields"
          ],
          "properties": {
            "slot": { "$ref": "#/$defs/id" },
            "credentialType": { "const": "http_header_auth" },
            "displayName": {
              "$ref": "#/$defs/httpCredentialMetadataText"
            },
            "metadataFields": {
              "type": "object",
              "additionalProperties": false,
              "required": ["header_name"],
              "properties": {
                "header_name": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 128,
                  "pattern": "^[!#$%&'*+.^_`|~0-9A-Za-z-]+$"
                }
              }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "slot",
            "credentialType",
            "displayName",
            "metadataFields"
          ],
          "properties": {
            "slot": { "$ref": "#/$defs/id" },
            "credentialType": { "const": "http_api_key" },
            "displayName": {
              "$ref": "#/$defs/httpCredentialMetadataText"
            },
            "metadataFields": {
              "type": "object",
              "additionalProperties": false,
              "required": ["placement", "parameter_name"],
              "properties": {
                "placement": { "enum": ["header", "query"] },
                "parameter_name": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 128,
                  "pattern": "^[!#$%&'*+.^_`|~0-9A-Za-z-]+$"
                }
              }
            }
          }
        }
      ]
    },
    "profile": {
      "type": "object",
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "mode": { "const": "live" }
            },
            "required": ["mode"]
          },
          "then": {
            "properties": {
              "httpCredentialFixtures": {
                "type": "array",
                "maxItems": 0
              }
            }
          }
        }
      ],
      "required": [
        "surface",
        "target",
        "mode",
        "permissionScenario",
        "environment",
        "artifacts"
      ],
      "properties": {
        "surface": {
          "$ref": "#/$defs/contributionId"
        },
        "target": {
          "enum": [
            "desktop",
            "mobile",
            "quickjs",
            "worker",
            "node",
            "workflow-host"
          ]
        },
        "mode": {
          "enum": ["surface", "live"]
        },
        "permissionScenario": {
          "$ref": "#/$defs/permissionScenario"
        },
        "deniedActions": {
          "type": "array",
          "maxItems": 128,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/actionId"
          }
        },
        "fixtureFiles": {
          "$ref": "#/$defs/fileSet"
        },
        "httpRouteFiles": {
          "$ref": "#/$defs/fileSet"
        },
        "requiredOrigins": {
          "$ref": "#/$defs/originSet"
        },
        "requiredEffects": {
          "type": "array",
          "maxItems": 128,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/effectToken"
          }
        },
        "nativeServices": {
          "type": "boolean",
          "default": false,
          "description": "Explicitly enable session-scoped native services for a reviewed Live TAP run."
        },
        "credentialSlots": {
          "type": "array",
          "maxItems": 64,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/id"
          }
        },
        "httpCredentialFixtures": {
          "type": "array",
          "maxItems": 64,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/httpCredentialFixture"
          }
        },
        "environment": {
          "$ref": "#/$defs/environment"
        },
        "artifacts": {
          "$ref": "#/$defs/artifacts"
        }
      }
    },
    "matrixEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "profile"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "profile": {
          "$ref": "#/$defs/id"
        },
        "testMatch": {
          "$ref": "#/$defs/testMatch"
        },
        "capabilities": {
          "type": "array",
          "maxItems": 128,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/capabilityId"
          }
        }
      }
    },
    "waiver": {
      "type": "object",
      "additionalProperties": false,
      "required": ["capability", "reason"],
      "properties": {
        "capability": {
          "$ref": "#/$defs/capabilityId"
        },
        "reason": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048,
          "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
        },
        "issue": {
          "type": "string",
          "minLength": 8,
          "maxLength": 2048,
          "format": "uri"
        },
        "expiresAt": {
          "type": "string",
          "maxLength": 64,
          "format": "date-time"
        }
      }
    }
  }
}
