mirror of
https://github.com/TautvydasDerzinskas/Thingport.git
synced 2026-10-18 20:59:55 -05:00
fix: make link-authors test deterministic
The test was timing-dependent: it checked for a 409 while the first run is still going, but completion depended on when the mock fetch resolved. Replace the instant mock with a promise the test controls, making the assertion deterministic. Also save/restore the Thingiverse access token to prevent test isolation issues.
This commit is contained in:
1 parent
9d41c4dee3
commit
9ada95b0a2
1 file changed
+25
-1
@@ -265,17 +265,40 @@ describe("looking up authors no known author matches", () => {
|
||||
});
|
||||
|
||||
describe("Administration > Triggers > Link missing authors", () => {
|
||||
// Files share one database; leave the token as this file found it, even when a test fails.
|
||||
let previousToken: string | null;
|
||||
// Lets a run held open by a test finish, even when the test fails before letting it.
|
||||
let answer: (() => void) | null = null;
|
||||
|
||||
beforeAll(async () => {
|
||||
previousToken = await getThingiverseAccessToken();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
answer?.();
|
||||
answer = null;
|
||||
global.fetch = originalFetch;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await setThingiverseAccessToken(previousToken);
|
||||
});
|
||||
|
||||
it("is admin-only", async () => {
|
||||
expect((await request(app).get("/api/admin/triggers/link-authors").set(auth(memberToken))).status).toBe(403);
|
||||
expect((await request(app).post("/api/admin/triggers/link-authors").set(auth(memberToken))).status).toBe(403);
|
||||
});
|
||||
|
||||
it("says what it can link -- which decides whether it's shown -- then links it in the background", async () => {
|
||||
mockFetch({});
|
||||
// One author to look up on Thingiverse, whose answer waits until the second start has been tried: the run
|
||||
// otherwise finishes in milliseconds, and whether it's still going when asked again is down to timing.
|
||||
const answered = new Promise<void>((resolve) => (answer = resolve));
|
||||
global.fetch = vi.fn<() => Promise<Response>>(async () => {
|
||||
await answered;
|
||||
return new Response("not found", { status: 404 });
|
||||
}) as unknown as typeof fetch;
|
||||
await setThingiverseAccessToken("test-token");
|
||||
await unattributedPrint("trigger-wait", "thingiverse", `Somebody Else ${stamp}`);
|
||||
const author = await prisma.author.create({
|
||||
data: {
|
||||
id: `thingiverse:trigger-${stamp}`,
|
||||
@@ -294,6 +317,7 @@ describe("Administration > Triggers > Link missing authors", () => {
|
||||
expect(started.status).toBe(200);
|
||||
expect(started.body.run.running).toBe(true);
|
||||
expect((await request(app).post("/api/admin/triggers/link-authors").set(auth(adminToken))).status).toBe(409);
|
||||
answer?.();
|
||||
|
||||
let run = started.body.run;
|
||||
for (let i = 0; i < 100 && run.running; i++) {
|
||||
|
||||
Reference in new issue
Block a user