/* eslint-disable no-unused-vars */
import _ from "lodash"
import { assert } from "chai"

import { DASHBOARD_RESPONSE } from "./test_constants"
import { makeDashboard } from "./factories/dashboard"

describe("constants", () => {
  const assertResponse = programs => {
    const programIds: Set<number> = new Set()
    const courseIds: Set<number> = new Set()
    const runIds: Set<number> = new Set()
    const courseKeys: Set<string> = new Set()
    for (const program of programs) {
      assert(!_.isNil(program.id), "Missing program id")
      assert(!programIds.has(program.id), `Duplicate program id ${program.id}`)
      programIds.add(program.id)

      const positionInProgram: Set<number> = new Set()

      for (const course of program.courses) {
        assert(
          !_.isNil(course.id),
          `Missing course id for program ${program.id}`
        )
        assert(!courseIds.has(course.id), `Duplicate course id ${course.id}`)
        courseIds.add(course.id)

        assert(
          !_.isNil(course.position_in_program),
          `Missing position_in_program for course ${course.id}`
        )
        assert(
          !positionInProgram.has(course.position_in_program),
          `Duplicate position for course ${course.id}`
        )
        positionInProgram.add(course.position_in_program)

        const positionInCourse: Set<number> = new Set()
        for (const run of course.runs) {
          assert(!_.isNil(run.id), `Missing run id for course ${course.id}`)
          assert(!runIds.has(run.id), `Duplicate run id ${run.id}`)
          runIds.add(run.id)

          assert(!_.isNil(run.position), `Missing position for run ${run.id}`)
          assert(
            !positionInCourse.has(run.position),
            `Duplicate position for run ${run.id}`
          )
          positionInCourse.add(run.position)
          assert(run.course_id, `Missing course_id for run ${run.id}`)
          assert(
            !courseKeys.has(run.course_id),
            `Duplicate course key ${run.course_id}`
          )
          courseKeys.add(run.course_id)
        }
      }
    }
  }

  describe("doesn't duplicate any id numbers within the same type of information", () => {
    it("for DASHBOARD_RESPONSE", () => {
      assertResponse(DASHBOARD_RESPONSE.programs)
    })

    it("for a response from a factory", () => {
      const dashboard = makeDashboard()
      assertResponse(dashboard.programs)
    })
  })
})
