{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rehearse.kurvball.ca/schema/v1/observation.schema.json",
  "title": "Rehearse v1: rehearse run --json",
  "description": "One observation: a scenario run once on one checkout, as one person, from one world. Stored raw; what is set aside is applied only when comparing. Tokens are scrubbed.",
  "type": "object",
  "required": [
    "v",
    "ok",
    "scenario",
    "checkout",
    "world",
    "person",
    "standins",
    "failures",
    "observed",
    "ms",
    "at",
    "rehearse"
  ],
  "properties": {
    "v": {
      "$ref": "common.schema.json#/$defs/v"
    },
    "ok": {
      "description": "Whether the scenario ran and its own checks held. false means it fails: a call's status or assert, a file run that exited non-zero, a flow step that stopped, a piece that threw.",
      "type": "boolean"
    },
    "scenario": {
      "$ref": "common.schema.json#/$defs/scenarioRef"
    },
    "checkout": {
      "$ref": "common.schema.json#/$defs/checkout"
    },
    "world": {
      "type": ["string", "null"]
    },
    "person": {
      "$ref": "common.schema.json#/$defs/person"
    },
    "standins": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/standin"
      }
    },
    "failures": {
      "type": "array",
      "items": {
        "$ref": "common.schema.json#/$defs/failure"
      }
    },
    "broke": {
      "description": "Set when something around the scenario broke, not the scenario: the checkout wasn't prepared, a service didn't come up, the sign-in or the world failed. Such a run is never cached.",
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "required": ["step", "says"],
          "properties": {
            "step": {
              "description": "Where it broke. More may come.",
              "type": "string",
              "examples": ["prepare", "overlay", "up", "ready", "signIn", "world", "driver", "runner"]
            },
            "says": {
              "type": "string"
            }
          }
        }
      ]
    },
    "observed": {
      "description": "What the kind observes; its shape depends on scenario.kind. null when broke is set and nothing was observed.",
      "oneOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/$defs/call"
        },
        {
          "$ref": "#/$defs/file"
        },
        {
          "$ref": "#/$defs/flow"
        },
        {
          "$ref": "#/$defs/piece"
        },
        {
          "$ref": "#/$defs/other"
        }
      ]
    },
    "artifacts": {
      "description": "Files the run kept: screenshots, outputs, logs.",
      "type": "array",
      "items": {
        "type": "object",
        "required": ["kind", "path"],
        "properties": {
          "kind": {
            "type": "string"
          },
          "path": {
            "type": "string"
          },
          "label": {
            "type": "string"
          }
        }
      }
    },
    "ms": {
      "type": "integer",
      "minimum": 0
    },
    "at": {
      "type": "string",
      "format": "date-time"
    },
    "rehearse": {
      "description": "The Rehearse version that ran it.",
      "type": "string"
    },
    "cached": {
      "description": "True when this observation was read from the baseline cache.",
      "type": "boolean"
    },
    "runner": {
      "description": "The kind runner's version that observed it: part of the cache key.",
      "type": "string"
    },
    "cacheKey": {
      "description": "The key it is cached under, when its checkout is a clean commit.",
      "type": ["string", "null"]
    }
  },
  "$defs": {
    "call": {
      "type": "object",
      "required": ["kind", "request", "who", "status"],
      "properties": {
        "kind": {
          "const": "call"
        },
        "request": {
          "description": "The method and path, with variables unfilled.",
          "type": "string"
        },
        "who": {
          "description": "Who it was sent as: a person's name, nobody, or a token for no one.",
          "type": "string"
        },
        "status": {
          "type": ["integer", "null"]
        },
        "contentType": {
          "type": ["string", "null"]
        },
        "body": {
          "description": "The answer's body: parsed JSON when it is JSON, else its text, clipped."
        },
        "tries": {
          "description": "Each try's status, for a call that retries.",
          "type": "array",
          "items": {
            "type": ["integer", "null"]
          }
        },
        "before": {
          "description": "How many earlier calls in its file ran first, for their captures.",
          "type": "integer"
        }
      }
    },
    "file": {
      "type": "object",
      "required": ["kind", "exit", "outputs"],
      "properties": {
        "kind": {
          "const": "file"
        },
        "exit": {
          "type": ["integer", "null"]
        },
        "log": {
          "description": "The last lines the run printed.",
          "type": "string"
        },
        "outputs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/output"
          }
        }
      }
    },
    "output": {
      "type": "object",
      "required": ["file", "layout"],
      "properties": {
        "file": {
          "type": "string"
        },
        "layout": {
          "type": "string",
          "examples": ["csv", "tsv", "json", "lines", "fixed", "zip", "missing"]
        },
        "columns": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "rows": {
          "type": "array",
          "items": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        },
        "lines": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "json": {},
        "entries": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/output"
          }
        }
      }
    },
    "flow": {
      "type": "object",
      "required": ["kind", "driver", "steps"],
      "properties": {
        "kind": {
          "const": "flow"
        },
        "driver": {
          "description": "service: the service's own driver. browser: Rehearse's browser. More may come.",
          "type": "string",
          "examples": ["service", "browser"]
        },
        "said": {
          "description": "Anything the driver couldn't do, such as signing in as a person.",
          "type": ["string", "null"]
        },
        "steps": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["n", "step", "ok"],
            "properties": {
              "n": {
                "type": "integer",
                "minimum": 1
              },
              "step": {
                "type": "object"
              },
              "ok": {
                "type": "boolean"
              },
              "says": {
                "type": ["string", "null"]
              },
              "tree": {
                "description": "The accessibility tree after the step.",
                "type": ["string", "null"]
              },
              "screenshot": {
                "type": ["string", "null"]
              }
            }
          }
        },
        "errors": {
          "description": "What the page logged as errors while the flow ran (Rehearse's browser only). Compared, as a piece's are.",
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "piece": {
      "type": "object",
      "required": ["kind", "schemes"],
      "properties": {
        "kind": {
          "const": "piece"
        },
        "storyId": {
          "description": "Its Storybook id, when its title could be read.",
          "type": ["string", "null"]
        },
        "schemes": {
          "type": "object",
          "propertyNames": {
            "enum": ["light", "dark"]
          },
          "additionalProperties": {
            "type": "object",
            "required": ["tree", "errors", "violations"],
            "properties": {
              "tree": {
                "type": ["string", "null"]
              },
              "threw": {
                "type": ["string", "null"]
              },
              "errors": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "violations": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": ["id", "help"],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "help": {
                      "type": "string"
                    },
                    "nodes": {
                      "type": "integer"
                    }
                  }
                }
              },
              "screenshot": {
                "type": ["string", "null"]
              },
              "pixels": {
                "description": "A hash of the screenshot, compared only when the manifest asks.",
                "type": ["string", "null"]
              }
            }
          }
        }
      }
    },
    "other": {
      "description": "A later or repository-defined kind.",
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": {
          "not": {
            "enum": ["call", "file", "flow", "piece"]
          }
        }
      }
    }
  },
  "if": {
    "properties": {
      "observed": {
        "type": "null"
      }
    },
    "required": ["observed"]
  },
  "then": {
    "required": ["broke"],
    "properties": {
      "broke": {
        "type": "object"
      }
    }
  }
}
