// seeds the api test db before a flow runs. mirrors bluesky's setupServer.js:
// the api /dev/seed route is env-guarded (404 in production) and wipes the
// e2e user's data each call so flows are deterministic.
// SCENARIOS is a comma list (e.g. "crumbs,trail"); E2E_EMAIL is the seeded user.

var apiUrl =
  typeof API_URL === 'string' && API_URL.length
    ? API_URL
    : 'http://localhost:4000'
var scenarios =
  typeof SCENARIOS === 'string' && SCENARIOS.length
    ? SCENARIOS.split(',')
    : []

// always start online: clear the e2e network kill-switch in case a prior
// offline flow crashed with it flipped, which would 503 every later flow
var network = http.post(apiUrl + '/dev/network', {
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({down: false}),
})
if (!network.ok)
  throw new Error('/dev/network failed with status ' + network.status)

var seed = http.post(apiUrl + '/dev/seed', {
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({email: E2E_EMAIL, scenarios: scenarios}),
})
// fail fast: a flow driven against an unseeded db produces baffling
// selector timeouts instead of this one-line cause
if (!seed.ok)
  throw new Error('/dev/seed failed with status ' + seed.status)
