Files
mtusi2026lab7/student-tests/shared-login-case.test.ts
T

164 lines
4.2 KiB
TypeScript

import { expect, test } from "bun:test";
import {
createSharedLoginCase,
type LoginObservation,
} from "../scripts/shared-login-case";
const aliases = {
api1: "11111111-1111-4111-8111-111111111111",
api2: "22222222-2222-4222-8222-222222222222",
};
function fixture() {
let count = 0,
controls = 0,
active = 0,
maxActive = 0;
const responses: LoginObservation[] = [];
return {
responses,
get controls() {
return controls;
},
get maxActive() {
return maxActive;
},
ports: {
now: () => 1000,
request: async (
replica: "api1" | "api2",
kind: "main" | "control",
): Promise<LoginObservation> => {
active++;
maxActive = Math.max(maxActive, active);
await Promise.resolve();
active--;
if (kind === "control") {
controls++;
return {
status: 201,
alias: aliases[replica],
actorCorrect: true,
retryAfter: null,
errorCode: null,
};
}
const index = count++,
result = responses[index] ?? {
status: index < 10 ? 201 : 429,
alias: aliases[replica],
actorCorrect: true,
retryAfter: index < 10 ? null : "60",
errorCode: index < 10 ? null : "rate_limited",
};
return result;
},
},
};
}
test("twenty requests in concurrent pairs prove one shared ten-attempt window and two other-account controls", async () => {
const f = fixture();
expect(await createSharedLoginCase(f.ports)(aliases)).toEqual({
requests: 20,
accepted: 10,
denied: 10,
controls: 2,
elapsedMilliseconds: 0,
});
expect(f.maxActive).toBe(2);
});
test.each([
{
name: "independent limits",
patch: { status: 201 },
code: "SHARED_LIMIT_NOT_PROVED",
index: 10,
},
{
name: "missing retry",
patch: { retryAfter: null },
code: "LOGIN_RETRY_AFTER_INVALID",
index: 10,
},
{
name: "wrong body",
patch: { errorCode: "unavailable" },
code: "LOGIN_REJECTION_BODY_INVALID",
index: 10,
},
{
name: "wrong instance",
patch: { alias: aliases.api2 },
code: "LOGIN_INSTANCE_MISMATCH",
index: 0,
},
{
name: "wrong actor",
patch: { actorCorrect: false },
code: "LOGIN_SUCCESS_ACTOR_MISMATCH",
index: 0,
},
{
name: "unexpected overload",
patch: { status: 503 },
code: "LOGIN_STATUS_UNEXPECTED",
index: 0,
},
])(
"reject $name before dependent control requests",
async ({ patch, code, index }) => {
const f = fixture();
f.responses[index] = {
status: index < 10 ? 201 : 429,
alias: index % 2 === 0 ? aliases.api1 : aliases.api2,
actorCorrect: true,
retryAfter: index < 10 ? null : "60",
errorCode: index < 10 ? null : "rate_limited",
...patch,
};
await expect(createSharedLoginCase(f.ports)(aliases)).rejects.toThrow(code);
expect(f.controls).toBe(0);
},
);
test("native failure preserves its original identity", async () => {
const f = fixture(),
error = Error("FIXTURE_NATIVE_HTTP");
f.ports.request = async () => {
throw error;
};
let actual;
try {
await createSharedLoginCase(f.ports)(aliases);
} catch (e) {
actual = e;
}
expect(actual).toBe(error);
});
test("expired main window cannot count a second rate window", async () => {
const f = fixture();
let read = 0;
f.ports.now = () => (read++ === 0 ? 1000 : 61000);
await expect(createSharedLoginCase(f.ports)(aliases)).rejects.toThrow(
"LOGIN_CASE_WINDOW_EXPIRED",
);
});
test("failed other-account control rejects", async () => {
const f = fixture(),
native = f.ports.request;
f.ports.request = async (r, k) =>
k === "control"
? {
status: 429,
alias: aliases[r],
actorCorrect: false,
retryAfter: "60",
errorCode: "rate_limited",
}
: native(r, k);
await expect(createSharedLoginCase(f.ports)(aliases)).rejects.toThrow(
"LOGIN_CONTROL_FAILED",
);
});
test("explicit ports have no process or network fallback", () =>
expect(() => createSharedLoginCase({} as never)).toThrow(
"LOGIN_CASE_PORTS_REQUIRED",
));