test: await completed sign-in in account lifecycle checks

This commit is contained in:
2026-10-25 23:04:31 +03:00
parent d2d9fcd09e
commit bd75467be8
+12 -3
View File
@@ -5,6 +5,7 @@ import { createHash } from "node:crypto";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { exportJWK, generateKeyPair, SignJWT } from "jose";
const require = createRequire(import.meta.url);
const { createChatGPT, fileStore, allowedAuthEndpoint, SCOPES } = require("../electron/chatgpt.cjs");
@@ -55,6 +56,14 @@ function fixture(options: { state?: any; expires?: number; timeoutMs?: number; t
return fetch(callback);
} };
}
// Callback HTTP success precedes asynchronous token verification and persistence.
async function waitForSignIn(service: { status(): Promise<{ pending: boolean }> }) {
const deadline = Date.now() + 5000;
while ((await service.status()).pending) {
assert(Date.now() < deadline, "sign-in did not settle within the fixture timeout");
await delay(5);
}
}
async function loggedIn(options: Parameters<typeof fixture>[0] = {}) {
const f = fixture(options);
await f.service.login();
@@ -450,7 +459,7 @@ test("repeated real verified sign-in creates one visible account while preservin
try {
const original = f.saved.registrations[0];
await f.service.login(); await f.callback({ client_id: "oaiapp_repeat" });
for (let i = 0; i < 100 && (await f.service.status()).pending; i++) await new Promise((r) => setImmediate(r));
await waitForSignIn(f.service);
const status = await f.service.status();
assert.equal(f.saved.registrations.length, 2); assert.equal(status.accounts.length, 1);
assert.equal(status.activeId, status.accounts[0].id); assert.notEqual(status.activeId, original.id);
@@ -595,7 +604,7 @@ test("one app-wide background preference defaults ON over legacy OFF, persists w
const id = f.saved.activeId;
await f.service.select(id); assert.equal((await f.service.status()).allowBackgroundUse, false);
await f.service.login(id); await f.callback();
for (let i = 0; i < 100 && (await f.service.status()).pending; i++) await new Promise((r) => setImmediate(r));
await waitForSignIn(f.service);
assert.equal(f.saved.allowBackgroundUse, false, "reconnecting cannot overwrite app preference");
assert.equal(f.saved.registrations[0].background, undefined, "new verified registrations omit obsolete per-account preference");
await f.service.disconnect(f.saved.activeId);
@@ -604,7 +613,7 @@ test("one app-wide background preference defaults ON over legacy OFF, persists w
try {
assert.equal((await restarted.service.status()).allowBackgroundUse, false, "zero-account restart preserves global OFF");
await restarted.service.login(); await restarted.callback();
for (let i = 0; i < 100 && (await restarted.service.status()).pending; i++) await new Promise((r) => setImmediate(r));
await waitForSignIn(restarted.service);
assert.equal(restarted.saved.allowBackgroundUse, false);
await restarted.service.consent(true); assert.equal(restarted.saved.allowBackgroundUse, true);
} finally { restarted.service.close(); }