Login_test.res

  1// test/Login_test.res
  2open VitestHelpers
  3
  4open Login
  5
  6describe("validateLoginForm", () => {
  7  test("Valid when both user and password provided", t => {
  8    let result = validateLoginForm(~user=Some("alice"), ~password=Some("secret"))
  9    t->expect(result)->toEqual(Valid)
 10  })
 11
 12  test("Invalid when user missing", t => {
 13    let result = validateLoginForm(~user=None, ~password=Some("secret"))
 14    t->expect(result)->toEqual(Invalid("Username or handle is required"))
 15  })
 16
 17  test("Invalid when password missing", t => {
 18    let result = validateLoginForm(~user=Some("alice"), ~password=None)
 19    t->expect(result)->toEqual(Invalid("Password is required"))
 20  })
 21
 22  test("Invalid when both missing", t => {
 23    let result = validateLoginForm(~user=None, ~password=None)
 24    t->expect(result)->toEqual(Invalid("Username and password are required"))
 25  })
 26})
 27
 28describe("validateSignupForm", () => {
 29  test("Valid when email and password provided", t => {
 30    let result = validateSignupForm(~user=Some("[email protected]"), ~handle=None, ~password=Some("secret"))
 31    t->expect(result)->toEqual(Valid)
 32  })
 33
 34  test("Valid when handle and password provided", t => {
 35    let result = validateSignupForm(~user=None, ~handle=Some("alice"), ~password=Some("secret"))
 36    t->expect(result)->toEqual(Valid)
 37  })
 38
 39  test("Valid when all three provided", t => {
 40    let result = validateSignupForm(
 41      ~user=Some("[email protected]"),
 42      ~handle=Some("alice"),
 43      ~password=Some("secret"),
 44    )
 45    t->expect(result)->toEqual(Valid)
 46  })
 47
 48  test("Invalid when neither email nor handle provided", t => {
 49    let result = validateSignupForm(~user=None, ~handle=None, ~password=Some("secret"))
 50    t->expect(result)->toEqual(Invalid("A handle or an email and a password are required"))
 51  })
 52
 53  test("Invalid when password missing", t => {
 54    let result = validateSignupForm(~user=Some("[email protected]"), ~handle=None, ~password=None)
 55    t->expect(result)->toEqual(Invalid("A handle or an email and a password are required"))
 56  })
 57})
 58
 59describe("parseUserFromJSON", () => {
 60  test("Parses complete user JSON with base36 id", t => {
 61    let json = JSON.Encode.object(
 62      Dict.fromArray([
 63        ("id", JSON.Encode.string("zvozorhhxc0")),
 64        ("email", JSON.Encode.string("[email protected]")),
 65        ("handle", JSON.Encode.string("alice")),
 66        ("registered", JSON.Encode.bool(true)),
 67      ]),
 68    )
 69    let result = parseUserFromJSON(json)
 70    t->expect(result)->toEqual(Some({id: "zvozorhhxc0", email: "[email protected]", handle: "alice", registered: true}))
 71  })
 72
 73  test("Ignores numeric id and returns None", t => {
 74    let json = JSON.Encode.object(
 75      Dict.fromArray([
 76        ("id", JSON.Encode.float(42.0)),
 77        ("email", JSON.Encode.string("[email protected]")),
 78        ("handle", JSON.Encode.string("alice")),
 79        ("registered", JSON.Encode.bool(false)),
 80      ]),
 81    )
 82    let result = parseUserFromJSON(json)
 83    t->expect(result)->toEqual(None)
 84  })
 85
 86  test("Returns None for missing id", t => {
 87    let json = JSON.Encode.object(
 88      Dict.fromArray([
 89        ("email", JSON.Encode.string("[email protected]")),
 90        ("handle", JSON.Encode.string("alice")),
 91        ("registered", JSON.Encode.bool(true)),
 92      ]),
 93    )
 94    let result = parseUserFromJSON(json)
 95    t->expect(result)->toEqual(None)
 96  })
 97
 98  test("Defaults optional fields", t => {
 99    let json = JSON.Encode.object(Dict.fromArray([("id", JSON.Encode.string("abc123"))]))
100    let result = parseUserFromJSON(json)
101    t->expect(result)->toEqual(Some({id: "abc123", email: "", handle: "", registered: false}))
102  })
103})