This commit is contained in:
Andras Schmelczer 2025-11-23 11:29:42 +00:00
parent 17dcfe300b
commit eab81bbbbc
2 changed files with 18 additions and 18 deletions

View file

@ -9,7 +9,7 @@ describe("FetchController", () => {
const createMockFetch = (shouldSleep: boolean) => const createMockFetch = (shouldSleep: boolean) =>
mock.fn(async () => { mock.fn(async () => {
if (shouldSleep) { if (shouldSleep) {
await sleep(50); await sleep(30);
} }
return Promise.resolve(new Response("OK", { status: 200 })); return Promise.resolve(new Response("OK", { status: 200 }));
}); });
@ -25,13 +25,12 @@ describe("FetchController", () => {
it("should allow fetch when canFetch is true", async () => { it("should allow fetch when canFetch is true", async () => {
const logger = new Logger(); const logger = new Logger();
const controller = new FetchController(true, logger); const controller = new FetchController(true, logger);
const mockFetch = createMockFetch(true); const mockFetch = createMockFetch(false);
const controlledFetch = controller.getControlledFetchImplementation( const controlledFetch = controller.getControlledFetchImplementation(
logger, logger,
mockFetch mockFetch
); );
mock.timers.tick(50);
const response = await controlledFetch("http://example.com"); const response = await controlledFetch("http://example.com");
assert.strictEqual(await response.text(), "OK"); assert.strictEqual(await response.text(), "OK");
@ -48,12 +47,12 @@ describe("FetchController", () => {
); );
const fetchPromise = controlledFetch("http://example.com"); const fetchPromise = controlledFetch("http://example.com");
mock.timers.tick(10);
assert.strictEqual(mockFetch.mock.calls.length, 0); assert.strictEqual(mockFetch.mock.calls.length, 0);
controller.canFetch = true; controller.canFetch = true;
await Promise.resolve();
mock.timers.tick(30);
mock.timers.tick(50);
const response = await fetchPromise; const response = await fetchPromise;
assert.strictEqual(await response.text(), "OK"); assert.strictEqual(await response.text(), "OK");
assert.strictEqual(mockFetch.mock.calls.length, 1); assert.strictEqual(mockFetch.mock.calls.length, 1);
@ -69,11 +68,11 @@ describe("FetchController", () => {
); );
const firstRequest = controlledFetch("http://example.com"); const firstRequest = controlledFetch("http://example.com");
assert.strictEqual(mockFetch.mock.calls.length, 1); // because firstRequest started before reset assert.strictEqual(mockFetch.mock.calls.length, 1);
controller.startReset();
const secondRequest = controlledFetch("http://example.com");
mock.timers.tick(50); controller.startReset();
const secondRequest = controlledFetch("http://example.com");
await assert.rejects( await assert.rejects(
firstRequest, firstRequest,
@ -83,13 +82,13 @@ describe("FetchController", () => {
secondRequest, secondRequest,
(error: unknown) => error instanceof SyncResetError (error: unknown) => error instanceof SyncResetError
); );
assert.strictEqual(mockFetch.mock.calls.length, 1); // because firstRequest started before reset assert.strictEqual(mockFetch.mock.calls.length, 1);
}); });
it("should allow fetch after reset finishes", async () => { it("should allow fetch after reset finishes", async () => {
const logger = new Logger(); const logger = new Logger();
const controller = new FetchController(true, logger); const controller = new FetchController(true, logger);
const mockFetch = createMockFetch(true); const mockFetch = createMockFetch(false);
const controlledFetch = controller.getControlledFetchImplementation( const controlledFetch = controller.getControlledFetchImplementation(
logger, logger,
mockFetch mockFetch
@ -98,7 +97,6 @@ describe("FetchController", () => {
controller.startReset(); controller.startReset();
controller.finishReset(); controller.finishReset();
mock.timers.tick(50);
const response = await controlledFetch("http://example.com"); const response = await controlledFetch("http://example.com");
assert.strictEqual(await response.text(), "OK"); assert.strictEqual(await response.text(), "OK");
}); });
@ -134,8 +132,10 @@ describe("FetchController", () => {
controller.finishReset(); controller.finishReset();
mock.timers.tick(50); const fetchPromise = controlledFetch("http://example.com");
const response = await controlledFetch("http://example.com"); mock.timers.tick(30);
const response = await fetchPromise;
assert.strictEqual(await response.text(), "OK"); assert.strictEqual(await response.text(), "OK");
}); });

View file

@ -25,7 +25,7 @@ export class SyncService {
public constructor( public constructor(
private readonly deviceId: string, private readonly deviceId: string,
private readonly connectionStatus: FetchController, private readonly fetchController: FetchController,
private readonly settings: Settings, private readonly settings: Settings,
private readonly logger: Logger, private readonly logger: Logger,
fetchImplementation: typeof globalThis.fetch = globalThis.fetch fetchImplementation: typeof globalThis.fetch = globalThis.fetch
@ -34,7 +34,7 @@ export class SyncService {
const unboundFetch: typeof globalThis.fetch = async (...args) => const unboundFetch: typeof globalThis.fetch = async (...args) =>
fetchImplementation(...args); fetchImplementation(...args);
this.client = this.connectionStatus.getControlledFetchImplementation( this.client = this.fetchController.getControlledFetchImplementation(
this.logger, this.logger,
unboundFetch unboundFetch
); );
@ -341,8 +341,8 @@ export class SyncService {
private getUrl(path: string): string { private getUrl(path: string): string {
const { vaultName, remoteUri } = this.settings.getSettings(); const { vaultName, remoteUri } = this.settings.getSettings();
const safeRemoteUri = remoteUri.replace(/\/+$/g, ""); const remoteUriWithoutTrailingSlash = remoteUri.replace(/\/+$/, "");
return `${safeRemoteUri}/vaults/${vaultName}${path}`; return `${remoteUriWithoutTrailingSlash}/vaults/${vaultName}${path}`;
} }
private getDefaultHeaders( private getDefaultHeaders(