import { describe, expect, test } from "vitest"
import { runTest } from "~/utils/test-helpers"

describe("pw.env.getResolve", () => {
  test("returns the correct value for an existing selected environment value", () => {
    return expect(
      runTest(
        `
          const data = pw.env.getResolve("a")
          pw.expect(data).toBe("b")
      `,
        {
          global: [],
          selected: [
            {
              key: "a",
              currentValue: "b",
              initialValue: "b",
              secret: false,
            },
          ],
        }
      )()
    ).resolves.toEqualRight([
      expect.objectContaining({
        expectResults: [
          {
            status: "pass",
            message: "Expected 'b' to be 'b'",
          },
        ],
      }),
    ])
  })

  test("returns the correct value for an existing global environment value", () => {
    return expect(
      runTest(
        `
          const data = pw.env.getResolve("a")
          pw.expect(data).toBe("b")
      `,
        {
          global: [
            {
              key: "a",
              currentValue: "b",
              initialValue: "b",
              secret: false,
            },
          ],
          selected: [],
        }
      )()
    ).resolves.toEqualRight([
      expect.objectContaining({
        expectResults: [
          {
            status: "pass",
            message: "Expected 'b' to be 'b'",
          },
        ],
      }),
    ])
  })

  test("returns undefined for a key that is not present in both selected or environment", () => {
    return expect(
      runTest(
        `
          const data = pw.env.getResolve("a")
          pw.expect(data).toBe(undefined)
      `,
        {
          global: [],
          selected: [],
        }
      )()
    ).resolves.toEqualRight([
      expect.objectContaining({
        expectResults: [
          {
            status: "pass",
            message: "Expected 'undefined' to be 'undefined'",
          },
        ],
      }),
    ])
  })

  test("returns the value defined in selected environment if it is also present in global", () => {
    return expect(
      runTest(
        `
          const data = pw.env.getResolve("a")
          pw.expect(data).toBe("selected val")
      `,
        {
          global: [
            {
              key: "a",
              currentValue: "global val",
              initialValue: "global val",
              secret: false,
            },
          ],
          selected: [
            {
              key: "a",
              currentValue: "selected val",
              initialValue: "selected val",
              secret: false,
            },
          ],
        }
      )()
    ).resolves.toEqualRight([
      expect.objectContaining({
        expectResults: [
          {
            status: "pass",
            message: "Expected 'selected val' to be 'selected val'",
          },
        ],
      }),
    ])
  })

  test("resolve environment values", () => {
    return expect(
      runTest(
        `
          const data = pw.env.getResolve("a")
          pw.expect(data).toBe("there")
      `,
        {
          global: [],
          selected: [
            {
              key: "a",
              currentValue: "<<hello>>",
              initialValue: "<<hello>>",
              secret: false,
            },
            {
              key: "hello",
              currentValue: "there",
              initialValue: "there",
              secret: false,
            },
          ],
        }
      )()
    ).resolves.toEqualRight([
      expect.objectContaining({
        expectResults: [
          {
            status: "pass",
            message: "Expected 'there' to be 'there'",
          },
        ],
      }),
    ])
  })

  test("returns unresolved value on infinite loop in resolution", () => {
    return expect(
      runTest(
        `
          const data = pw.env.getResolve("a")
          pw.expect(data).toBe("<<hello>>")
      `,
        {
          global: [],
          selected: [
            {
              key: "a",
              currentValue: "<<hello>>",
              initialValue: "<<hello>>",
              secret: false,
            },
            {
              key: "hello",
              currentValue: "<<a>>",
              initialValue: "<<a>>",
              secret: false,
            },
          ],
        }
      )()
    ).resolves.toEqualRight([
      expect.objectContaining({
        expectResults: [
          {
            status: "pass",
            message: "Expected '<<hello>>' to be '<<hello>>'",
          },
        ],
      }),
    ])
  })

  test("errors if the key is not a string", () => {
    return expect(
      runTest(
        `
          const data = pw.env.getResolve(5)
      `,
        {
          global: [],
          selected: [],
        }
      )()
    ).resolves.toBeLeft()
  })
})
