


























@@ -3,6 +3,32 @@ import { describe, expect, it } from "vitest";
33import { resolveDiscordUserAllowlist } from "./resolve-users.js";
44import { jsonResponse, urlToString } from "./test-http-helpers.js";
556+type DiscordAllowlistResult = Awaited<ReturnType<typeof resolveDiscordUserAllowlist>>[number];
7+8+function expectResolvedUser(
9+result: DiscordAllowlistResult | undefined,
10+expected: { id: string; input?: string; name?: string },
11+) {
12+if (!result) {
13+throw new Error("expected Discord allowlist result");
14+}
15+expect(result.resolved).toBe(true);
16+expect(result.id).toBe(expected.id);
17+if (expected.input !== undefined) {
18+expect(result.input).toBe(expected.input);
19+}
20+if (expected.name !== undefined) {
21+expect(result.name).toBe(expected.name);
22+}
23+}
24+25+function expectUnresolvedUser(result: DiscordAllowlistResult | undefined) {
26+if (!result) {
27+throw new Error("expected Discord allowlist result");
28+}
29+expect(result.resolved).toBe(false);
30+}
31+632function createGuildListProbeFetcher() {
733let guildsCalled = false;
834const fetcher = withFetchPreconnect(async (input: RequestInfo | URL) => {
@@ -78,8 +104,8 @@ describe("resolveDiscordUserAllowlist", () => {
78104});
7910580106expect(results).toHaveLength(2);
81-expect(results[0]).toMatchObject({ resolved: true, id: "111" });
82-expect(results[1]).toMatchObject({ resolved: true, id: "222" });
107+expectResolvedUser(results[0], { id: "111" });
108+expectResolvedUser(results[1], { id: "222" });
83109expect(wasGuildsCalled()).toBe(false);
84110});
85111@@ -129,12 +155,7 @@ describe("resolveDiscordUserAllowlist", () => {
129155130156expect(guildsCalled).toBe(true);
131157expect(results).toHaveLength(1);
132-expect(results[0]).toMatchObject({
133-input: "alice",
134-resolved: true,
135-id: "u1",
136-name: "alice",
137-});
158+expectResolvedUser(results[0], { input: "alice", id: "u1", name: "alice" });
138159});
139160140161it("fetches guilds only once for multiple username entries", async () => {
@@ -166,8 +187,8 @@ describe("resolveDiscordUserAllowlist", () => {
166187167188expect(guildsCallCount).toBe(1);
168189expect(results).toHaveLength(2);
169-expect(results[0]).toMatchObject({ resolved: true, id: "u-alice" });
170-expect(results[1]).toMatchObject({ resolved: true, id: "u-bob" });
190+expectResolvedUser(results[0], { id: "u-alice" });
191+expectResolvedUser(results[1], { id: "u-bob" });
171192});
172193173194it("handles mixed ids and usernames — ids resolve even if guilds fail", async () => {
@@ -190,8 +211,8 @@ describe("resolveDiscordUserAllowlist", () => {
190211});
191212192213expect(results).toHaveLength(2);
193-expect(results[0]).toMatchObject({ resolved: true, id: "123456789012345678" });
194-expect(results[1]).toMatchObject({ resolved: true, id: "999" });
214+expectResolvedUser(results[0], { id: "123456789012345678" });
215+expectResolvedUser(results[1], { id: "999" });
195216});
196217197218it("returns unresolved for empty/blank entries", async () => {
@@ -206,8 +227,8 @@ describe("resolveDiscordUserAllowlist", () => {
206227});
207228208229expect(results).toHaveLength(2);
209-expect(results[0]).toMatchObject({ resolved: false });
210-expect(results[1]).toMatchObject({ resolved: false });
230+expectUnresolvedUser(results[0]);
231+expectUnresolvedUser(results[1]);
211232});
212233213234it("returns all unresolved when token is empty", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。