import { Client, ReqResponse, SessionId } from "./client";

class MockClient implements Client {
    users: { [_: string]: string }

    constructor() {
        this.users = { 'vakarian': 'aoeu', 'shephard': 'aoeu' }
    }

    async auth(username: string, password: string): Promise<ReqResponse<SessionId>> {
        if (this.users[username] == password) {
            if (username == 'shephard') {
                return { type: 'password-reset' }
            }
            return { type: "ok", content: "auth-successful" }
        } else {
            return { type: "error", content: 401 }
        }
    }
}

describe("what happens in an authentication", () => {
    it("handles a successful response", async () => {
        let client = new MockClient()
        let response = await client.auth("vakarian", "aoeu")
        expect(response).toEqual({ type: "ok", content: "auth-successful" })
    })

    it("handles an authentication failure", async () => {
        let client = new MockClient();
        {
            let response = await client.auth("vakarian", "")
            expect(response).toEqual({ type: "error", content: 401 });
        }

        {
            let response = await client.auth("grunt", "")
            expect(response).toEqual({ type: "error", content: 401 });
        }
    })

    it("handles a password-reset condition", async () => {
        let client = new MockClient();
        {
            let response = await client.auth("shephard", "aoeu")
            expect(response).toEqual({ type: "password-reset" });
        }
        {
            let response = await client.auth("shephard", "")
            expect(response).toEqual({ type: "error", content: 401 });
        }
    })
})