{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rehearse.kurvball.ca/schema/v1/manifest.schema.json",
  "title": "rehearse.yaml",
  "description": "What a repository has: the branch production runs, its services, people, worlds, stand-ins and scenarios. Every block is optional. Commands run in the checkout, with placeholders such as {{port}}, {{url}}, {{checkout}}, {{manifest}}, {{world}}, {{person}} and {{scratch}} filled in.",
  "type": "object",
  "properties": {
    "version": { "description": "The manifest's version. Only 1 exists.", "const": 1 },
    "base": {
      "description": "The branch production runs, main or dev. Comparisons use it as the base unless told otherwise.",
      "type": "string",
      "minLength": 1,
      "default": "main"
    },
    "checkout": {
      "description": "The checkout to run in when none is given, relative to this file. Useful when the manifest is kept outside the product.",
      "type": "string"
    },
    "prepare": {
      "description": "Run once in a fresh checkout of a base commit, before anything else: installing dependencies, building.",
      "$ref": "#/$defs/command"
    },
    "overlay": {
      "description": "Files copied into each checkout after prepare, for a manifest kept outside the product. In a repository that adopts Rehearse, commit them instead.",
      "type": "array",
      "items": {
        "type": "object",
        "required": ["from", "to"],
        "properties": {
          "from": { "description": "A file or folder, relative to this file.", "type": "string" },
          "to": { "description": "Where it goes, relative to the checkout.", "type": "string" }
        },
        "additionalProperties": false
      }
    },
    "services": {
      "description": "What can come up: a server, an app, a daemon.",
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/name" },
      "additionalProperties": { "$ref": "#/$defs/service" }
    },
    "signIn": {
      "description": "A command that prints the people you can sign in as, with their tokens, in the sign-in shape: {\"people\": {\"name\": \"token\" | {\"token\": ..., ...}}, \"variables\": {...}}.",
      "oneOf": [
        { "type": "string", "minLength": 1 },
        {
          "type": "object",
          "required": ["command"],
          "properties": {
            "command": { "type": "string", "minLength": 1 },
            "service": { "description": "The service that must be up for it to run.", "type": "string" },
            "cwd": { "type": "string" }
          },
          "additionalProperties": false
        }
      ]
    },
    "people": {
      "description": "Who you can sign in as.",
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/name", "not": { "enum": ["you", "nobody", "api"] } },
      "additionalProperties": { "$ref": "#/$defs/person" }
    },
    "worlds": {
      "description": "Named starting states. A world extends another and adds to it.",
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/name" },
      "additionalProperties": { "$ref": "#/$defs/world" }
    },
    "standins": {
      "description": "Fakes for what can't run locally, chosen at run time and named in every result.",
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/name" },
      "additionalProperties": { "$ref": "#/$defs/standin" }
    },
    "scenarios": {
      "description": "Where the scenarios are, by kind. A path that starts with ./ or ../ is read beside this file, the same on both sides; any other path is read from each checkout.",
      "type": "object",
      "properties": {
        "pieces": { "$ref": "#/$defs/pieces" },
        "flows": { "$ref": "#/$defs/flows" },
        "calls": { "$ref": "#/$defs/calls" },
        "files": { "$ref": "#/$defs/files" }
      },
      "additionalProperties": false
    },
    "defaults": {
      "description": "What a scenario runs with when it says nothing.",
      "type": "object",
      "properties": {
        "service": { "type": "string" },
        "world": { "type": "string" },
        "person": { "type": "string" }
      },
      "additionalProperties": false
    },
    "setAside": { "$ref": "#/$defs/setAside" },
    "affected": {
      "description": "How rehearse list --affected finds what a change reaches.",
      "type": "object",
      "properties": {
        "graph": {
          "description": "A command that prints the dependents of the files appended to it, as schema/v1/dependents.schema.json says. By default, Ashlar's when Ashlar is present.",
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "budget": {
      "description": "How much may run at once on this machine.",
      "type": "object",
      "properties": {
        "services": {
          "description": "Sets of services up at once. Default 1.",
          "type": "integer",
          "minimum": 1,
          "maximum": 4
        },
        "checkouts": {
          "description": "Base checkouts kept under .rehearse/checkouts. Default 3.",
          "type": "integer",
          "minimum": 1
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false,
  "$defs": {
    "name": { "type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_-]*$" },
    "command": { "type": "string", "minLength": 1 },
    "env": {
      "type": "object",
      "propertyNames": { "pattern": "^[A-Za-z_][A-Za-z0-9_]*$" },
      "additionalProperties": { "type": ["string", "number", "boolean"] }
    },
    "service": {
      "type": "object",
      "required": ["up"],
      "properties": {
        "describe": { "type": "string" },
        "up": {
          "description": "Brings it up. It may keep running, or start a daemon and exit 0.",
          "$ref": "#/$defs/command"
        },
        "cwd": { "description": "Where up runs, relative to the checkout.", "type": "string" },
        "port": {
          "description": "The port it listens on; auto picks a free one and gives it as {{port}}.",
          "oneOf": [{ "type": "integer", "minimum": 1, "maximum": 65535 }, { "const": "auto" }]
        },
        "url": {
          "description": "Where it answers. Default http://127.0.0.1:{{port}} when it has a port.",
          "type": "string"
        },
        "ready": {
          "description": "How Rehearse knows it is ready: a URL that answers, a command that exits 0, or a line in its log.",
          "oneOf": [
            { "type": "string", "minLength": 1 },
            {
              "type": "object",
              "properties": {
                "http": { "type": "string" },
                "command": { "type": "string" },
                "log": {
                  "description": "A regular expression its output matches when ready.",
                  "type": "string"
                }
              },
              "minProperties": 1,
              "maxProperties": 1,
              "additionalProperties": false
            }
          ]
        },
        "timeout": {
          "description": "Seconds to wait until ready. Default 60.",
          "type": "number",
          "exclusiveMinimum": 0
        },
        "down": {
          "description": "Stops it. By default its process group is ended.",
          "$ref": "#/$defs/command"
        },
        "env": { "$ref": "#/$defs/env" },
        "isolate": {
          "description": "How two copies (base and head) keep apart: a scratch folder per copy, given through an environment variable.",
          "type": "object",
          "required": ["env"],
          "properties": { "env": { "type": "string" } },
          "additionalProperties": false
        },
        "drive": {
          "description": "The service's own driver for flows and rehearse drive, such as 'pnpm drive'. It takes the drive steps and answers in the drive result JSON (drive.schema.json).",
          "$ref": "#/$defs/command"
        }
      },
      "additionalProperties": false
    },
    "person": {
      "type": "object",
      "properties": {
        "describe": { "type": "string" },
        "real": {
          "description": "How real the person is: local (a sign-in the local services trust), stand-in (a fake identity), real (a real account).",
          "enum": ["local", "stand-in", "real"],
          "default": "local"
        },
        "default": { "type": "boolean" },
        "token": {
          "description": "Where this person's credential comes from when signIn doesn't give it.",
          "type": "object",
          "properties": {
            "command": { "type": "string" },
            "env": { "type": "string" }
          },
          "minProperties": 1,
          "maxProperties": 1,
          "additionalProperties": false
        },
        "browser": {
          "description": "How a browser signs in as this person: a header on every request, values put in storage, or sign-in steps. {{token}} is the person's token.",
          "type": "object",
          "properties": {
            "headers": { "type": "object", "additionalProperties": { "type": "string" } },
            "storage": { "type": "object", "additionalProperties": { "type": "string" } },
            "cookies": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["name", "value"],
                "properties": {
                  "name": { "type": "string" },
                  "value": { "type": "string" },
                  "path": { "type": "string" }
                },
                "additionalProperties": false
              }
            },
            "steps": { "$ref": "flow.schema.json#/properties/steps" }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "world": {
      "type": "object",
      "properties": {
        "describe": { "type": "string" },
        "extends": { "description": "The world this one builds on.", "type": "string" },
        "add": {
          "description": "Adds this world on top of the one it extends, once services are up.",
          "$ref": "#/$defs/command"
        },
        "before": {
          "description": "Adds this world before services come up, such as files the services read when they start. No service is up yet, so {{port}}, {{url}} and {{scratch}} are not filled.",
          "$ref": "#/$defs/command"
        },
        "default": { "type": "boolean" }
      },
      "additionalProperties": false
    },
    "standin": {
      "type": "object",
      "required": ["replaces"],
      "properties": {
        "replaces": { "description": "What it stands in for.", "type": "string" },
        "by": { "description": "What plays it: a file, a package, a command.", "type": "string" },
        "fidelity": {
          "description": "fake: written by hand. recorded: replays real answers. local: the real thing, run locally.",
          "enum": ["fake", "recorded", "local"],
          "default": "fake"
        },
        "services": {
          "description": "The services it applies to. Default all.",
          "type": "array",
          "items": { "type": "string" }
        },
        "env": { "description": "Set on those services while the stand-in is used.", "$ref": "#/$defs/env" },
        "real": {
          "description": "How to use the real dependency instead (--real <name>). Without it, the real one can't be chosen locally.",
          "type": "object",
          "properties": { "env": { "$ref": "#/$defs/env" } },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "paths": {
      "description": "Globs: * stops at /, ** crosses folders. Or entries that also say which files they cover.",
      "type": "array",
      "items": {
        "oneOf": [
          { "type": "string", "minLength": 1 },
          {
            "type": "object",
            "required": ["path"],
            "properties": {
              "path": { "type": "string", "minLength": 1 },
              "covers": {
                "description": "Files whose change reaches these scenarios, for rehearse list --affected.",
                "type": "array",
                "items": { "type": "string" }
              }
            },
            "additionalProperties": false
          }
        ]
      }
    },
    "pieces": {
      "oneOf": [
        { "$ref": "#/$defs/paths" },
        {
          "type": "object",
          "required": ["paths"],
          "properties": {
            "paths": { "$ref": "#/$defs/paths" },
            "preview": {
              "description": "A Storybook-style preview module (decorators, parameters, global styles), relative to the piece's package.",
              "type": "string"
            },
            "styles": {
              "description": "A stylesheet every piece loads, relative to the piece's package.",
              "type": "string"
            },
            "config": {
              "description": "The Vite config to use, relative to the piece's package.",
              "type": "string"
            },
            "schemes": {
              "description": "The colour schemes each piece is observed in. Default light.",
              "type": "array",
              "items": { "enum": ["light", "dark"] },
              "minItems": 1,
              "uniqueItems": true
            },
            "compare": {
              "description": "What is compared. Default tree, errors and violations; screenshot only when named.",
              "type": "array",
              "items": { "enum": ["tree", "errors", "violations", "screenshot"] },
              "minItems": 1,
              "uniqueItems": true
            },
            "viewport": {
              "type": "object",
              "required": ["width", "height"],
              "properties": { "width": { "type": "integer" }, "height": { "type": "integer" } },
              "additionalProperties": false
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "flows": {
      "oneOf": [
        { "$ref": "#/$defs/paths" },
        {
          "type": "object",
          "required": ["paths"],
          "properties": {
            "paths": { "$ref": "#/$defs/paths" },
            "service": { "description": "The service flows open when they name none.", "type": "string" }
          },
          "additionalProperties": false
        }
      ]
    },
    "calls": {
      "oneOf": [
        { "$ref": "#/$defs/paths" },
        {
          "type": "object",
          "required": ["paths"],
          "properties": {
            "paths": { "$ref": "#/$defs/paths" },
            "service": {
              "description": "The service whose URL is {{api}}. Default the one named api, else the first with a URL.",
              "type": "string"
            },
            "variables": {
              "description": "More variables every call file can use.",
              "type": "object",
              "additionalProperties": { "type": "string" }
            },
            "unordered": {
              "description": "Compare arrays in answers without their order.",
              "type": "boolean"
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "files": {
      "oneOf": [
        { "$ref": "#/$defs/paths" },
        {
          "type": "object",
          "required": ["paths"],
          "properties": { "paths": { "$ref": "#/$defs/paths" } },
          "additionalProperties": false
        }
      ]
    },
    "setAside": {
      "description": "More to set aside before comparing, and what never to set aside.",
      "type": "object",
      "properties": {
        "keys": {
          "description": "Regular expressions over JSON keys whose values are set aside.",
          "type": "array",
          "items": { "type": "string" }
        },
        "values": {
          "description": "Regular expressions over values (or text) that are set aside.",
          "type": "array",
          "items": { "type": "string" }
        },
        "keep": {
          "description": "JSON paths or column names never set aside, such as $.expiresAt.",
          "type": "array",
          "items": { "type": "string" }
        }
      },
      "additionalProperties": false
    }
  }
}
