const setup = require("./utilities")
const { FilterConditions } = require("../steps/filter")

describe("test the filter logic", () => {
  async function checkFilter(field, condition, value, pass = true) {
    let res = await setup.runStep(setup.actions.FILTER.stepId,
      { field, condition, value }
    )
    expect(res.success).toEqual(pass)
  }

  it("should be able test equality", async () => {
    await checkFilter("hello", FilterConditions.EQUAL, "hello", true)
    await checkFilter("hello", FilterConditions.EQUAL, "no", false)
  })

  it("should be able to test greater than", async () => {
    await checkFilter(10, FilterConditions.GREATER_THAN, 5, true)
    await checkFilter(10, FilterConditions.GREATER_THAN, 15, false)
  })

  it("should be able to test less than", async () => {
    await checkFilter(5, FilterConditions.LESS_THAN, 10, true)
    await checkFilter(15, FilterConditions.LESS_THAN, 10, false)
  })

  it("should be able to in-equality", async () => {
    await checkFilter("hello", FilterConditions.NOT_EQUAL, "no", true)
    await checkFilter(10, FilterConditions.NOT_EQUAL, 10, false)
  })

  it("check number coercion", async () => {
    await checkFilter("10", FilterConditions.GREATER_THAN, "5", true)
  })

  it("check date coercion", async () => {
    await checkFilter(
      (new Date()).toISOString(),
      FilterConditions.GREATER_THAN,
      (new Date(-10000)).toISOString(),
      true
    )
  })

  it("check objects always false", async () => {
    await checkFilter({}, FilterConditions.EQUAL, {}, false)
  })
})