37 lines
1.7 KiB
TypeScript
37 lines
1.7 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { formatDiscordNickname, formatManagedDiscordNickname, lookupJavaProfile } from "../src/index";
|
|
|
|
describe("Java Edition profiles", () => {
|
|
it("returns Mojang's canonical UUID and username", async () => {
|
|
const request = vi.fn<typeof fetch>().mockResolvedValue(
|
|
new Response(JSON.stringify({ id: "069a79f444e94726a5befca90e38aaf5", name: "Notch" }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
}),
|
|
);
|
|
|
|
await expect(lookupJavaProfile("notch", request)).resolves.toEqual({
|
|
uuid: "069a79f444e94726a5befca90e38aaf5",
|
|
username: "Notch",
|
|
});
|
|
});
|
|
|
|
it("returns null when Mojang does not recognize the username", async () => {
|
|
const request = vi.fn<typeof fetch>().mockResolvedValue(new Response(null, { status: 204 }));
|
|
await expect(lookupJavaProfile("UnknownPlayer", request)).resolves.toBeNull();
|
|
});
|
|
|
|
it("preserves the Minecraft username while fitting Discord's nickname limit", () => {
|
|
expect(formatDiscordNickname("Alexandria Catherine", "SixteenCharName1")).toBe(
|
|
"Alexandria Ca (SixteenCharName1)",
|
|
);
|
|
expect(formatDiscordNickname("Sam", "Notch")).toBe("Sam (Notch)");
|
|
});
|
|
|
|
it("marks the Discord nickname TBD when the user has no Minecraft account", () => {
|
|
expect(formatManagedDiscordNickname("Alexandria Catherine", null)).toBe("Alexandria Catherine (TBD)");
|
|
expect(formatManagedDiscordNickname("A name that is definitely longer than Discord allows", null).length).toBeLessThanOrEqual(32);
|
|
expect(formatManagedDiscordNickname("A name that is definitely longer than Discord allows", null)).toMatch(/ \(TBD\)$/);
|
|
});
|
|
});
|