{
  "version": 14,
  "name": "23_run_time_limit",
  "description": "Example: the Run time limit (Flow Settings). The flow starts a job and polls its status until it is done, but this job never finishes, so the polling loop would run forever. The flow's Run time limit of 15 s stops it: the run fails with \"Run time limit reached\" instead of hanging, which is what keeps a CI job from hanging too (exported scripts take --run-timeout). Expected result: the flow FAILS after about 15 s; the step after the loop never runs.",
  "globals": [
    {
      "name": "$baseUrl",
      "value": "https://httpbin.org"
    }
  ],
  "globalFunctions": "// Assertion helpers, built on check(label, pass, details).\n// Each records a pass / fail line in the log and the report.\n// A failed one fails its step when the step is done;\n// the code keeps running. Change them or add your own.\n// r defaults to the last response.\n\nfunction assertTrue(value, label = \"is true\") {\n  return check(label, Boolean(value), { actual: value, expected: true });\n}\n\nfunction assertEqual(actual, expected, label = \"equals \" + JSON.stringify(expected)) {\n  return check(label, isEqual(actual, expected), { actual, expected });\n}\n\nfunction assertNotEqual(actual, unexpected, label = \"does not equal \" + JSON.stringify(unexpected)) {\n  return check(label, !isEqual(actual, unexpected), { actual, expected: \"not \" + JSON.stringify(unexpected) });\n}\n\n// text contains a part, or a list contains an item\nfunction assertContains(haystack, needle, label = \"contains \" + JSON.stringify(needle)) {\n  const pass = Array.isArray(haystack) ? haystack.some((x) => isEqual(x, needle)) : String(haystack).includes(needle);\n  return check(label, pass, { actual: haystack, expected: needle });\n}\n\nfunction assertMatch(text, regex, label = \"matches \" + regex) {\n  return check(label, new RegExp(regex).test(String(text)), { actual: text, expected: String(regex) });\n}\n\nfunction assertLessThan(actual, limit, label = \"< \" + limit) {\n  return check(label, Number(actual) < Number(limit), { actual, expected: \"< \" + limit });\n}\n\nfunction assertGreaterThan(actual, limit, label = \"> \" + limit) {\n  return check(label, Number(actual) > Number(limit), { actual, expected: \"> \" + limit });\n}\n\n// expected: 200, \"2xx\", or a list such as [200, 201]\nfunction assertStatus(expected, r = response) {\n  const list = [].concat(expected).map(String);\n  const status = String(r && r.status);\n  const pass = list.some((e) => e === status || (/^[1-5]xx$/i.test(e) && e[0] === status[0]));\n  return check(\"status is \" + list.join(\" or \"), pass, { actual: r && r.status, expected });\n}\n\nfunction assertTimeBelow(ms, r = response) {\n  return check(\"time < \" + ms + \" ms\", Boolean(r) && r.timeMs < ms, { actual: r && r.timeMs, expected: \"< \" + ms });\n}\n\n// without expected, the header only has to exist\nfunction assertHeader(name, expected, r = response) {\n  const value = r ? r.headers[String(name).toLowerCase()] : undefined;\n  if (expected === undefined) return check(\"header \" + name + \" exists\", value !== undefined, { actual: value });\n  return check(\"header \" + name + \" equals \" + expected, String(value) === String(expected), { actual: value, expected });\n}\n\n// e.g. assertJsonPath(\"$.items[0].id\", 42); without expected, the value only has to exist\nfunction assertJsonPath(path, expected, r = response) {\n  const value = jsonPath(path, r);\n  if (expected === undefined) return check(path + \" exists\", value !== undefined, { actual: value });\n  return check(path + \" equals \" + JSON.stringify(expected), isEqual(value, expected), { actual: value, expected });\n}\n\n// schema: a JSON Schema object\nfunction assertSchema(schema, r = response) {\n  const result = validateSchema(r, schema);\n  return check(\"matches the JSON Schema\", result.valid, { error: result.errors || undefined });\n}\n",
  "libraries": [],
  "activeEnvironment": "",
  "auth": {
    "type": "none"
  },
  "settings": {
    "continueOnError": false,
    "timeoutMs": 10000,
    "runTimeoutMs": 15000
  },
  "data": {
    "file": ""
  },
  "cookies": [],
  "tags": [
    "example",
    "settings",
    "expected-failure"
  ],
  "steps": [
    {
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        }
      ],
      "id": "start-job",
      "method": "POST",
      "name": "Start a job",
      "type": "request",
      "url": "$baseUrl/anything/jobs",
      "body": "{\"report\":\"monthly\"}",
      "headers": [
        {
          "name": "Content-Type",
          "value": "application/json"
        }
      ]
    },
    {
      "id": "wait-for-job",
      "name": "Poll until the job is done (it never is)",
      "type": "code",
      "code": "// A real flow would stop when the job reports \"done\". This job never does,\n// so only the Run time limit (Flow Settings) ends the loop.\nlet attempt = 0;\nwhile (true) {\n  attempt++;\n  const r = await http($baseUrl + \"/anything/jobs/42\");\n  const state = JSON.parse(r.body).json?.state || \"running\";\n  log(\"attempt \" + attempt + \": \" + state);\n  if (state === \"done\") break;\n  await sleep(2000);\n}"
    },
    {
      "id": "use-result",
      "name": "Use the result (never reached)",
      "type": "code",
      "code": "log(\"the job finished\");"
    }
  ]
}
