Skip to content

Commit 6500d92

Browse files
committed
fix unit test
1 parent 00cef05 commit 6500d92

2 files changed

Lines changed: 19 additions & 24 deletions

File tree

src/app/service/service_worker/synchronize.test.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,6 @@ console.log("ok");`
905905
"push-uuid.user.js": "etag-user-js",
906906
});
907907
});
908-
909908
it("skips status and digest update when a push hits remote conflict", async () => {
910909
const conflict = new FileSystemError({
911910
provider: "webdav",
@@ -941,20 +940,16 @@ console.log("ok");`
941940
]),
942941
} as any
943942
);
943+
944944
vi.spyOn(service, "pushScript").mockRejectedValue(conflict);
945945
const updateDigestSpy = vi.spyOn(service, "updateFileDigest");
946-
const notificationSpy = vi.spyOn(chrome.notifications, "create").mockReturnValue("notification-id" as any);
946+
const notifySpy = vi.spyOn(service, "notifySyncFailed").mockImplementation(() => {});
947947

948948
await service.syncOnce(syncConfig, fs);
949949

950950
expect(updateDigestSpy).not.toHaveBeenCalled();
951951
expect(fs.create).not.toHaveBeenCalledWith("scriptcat-sync.json", expect.anything());
952-
expect(notificationSpy).toHaveBeenCalledWith(
953-
expect.objectContaining({
954-
title: "Script Sync Failed",
955-
message: expect.stringContaining("remote conflict"),
956-
})
957-
);
952+
expect(notifySpy).toHaveBeenCalledWith(true, 1);
958953
});
959954

960955
it("skips status and digest update when any push task fails", async () => {
@@ -987,20 +982,16 @@ console.log("ok");`
987982
]),
988983
} as any
989984
);
985+
990986
vi.spyOn(service, "pushScript").mockRejectedValue(error);
991987
const updateDigestSpy = vi.spyOn(service, "updateFileDigest");
992-
const notificationSpy = vi.spyOn(chrome.notifications, "create").mockReturnValue("notification-id" as any);
988+
const notifySpy = vi.spyOn(service, "notifySyncFailed").mockImplementation(() => {});
993989

994990
await service.syncOnce(syncConfig, fs);
995991

996992
expect(updateDigestSpy).not.toHaveBeenCalled();
997993
expect(fs.create).not.toHaveBeenCalledWith("scriptcat-sync.json", expect.anything());
998-
expect(notificationSpy).toHaveBeenCalledWith(
999-
expect.objectContaining({
1000-
title: "Script Sync Failed",
1001-
message: expect.stringContaining("cloud sync changes failed"),
1002-
})
1003-
);
994+
expect(notifySpy).toHaveBeenCalledWith(false, 1);
1004995
});
1005996

1006997
it("scriptInstall enters cloud_sync queue and updates digest after push", async () => {

src/app/service/service_worker/synchronize.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,18 @@ export class SynchronizeService {
364364
});
365365
}
366366

367+
public notifySyncFailed(hasConflict: any, rejectedCount: number) {
368+
this.logger.warn("skip status and digest update because cloud sync task failed", {
369+
conflict: hasConflict,
370+
failed: rejectedCount,
371+
});
372+
const title = i18n.t("notification.script_sync_failed");
373+
const message = hasConflict
374+
? i18n.t("notification.script_sync_conflict_desc")
375+
: i18n.t("notification.script_sync_failed_desc");
376+
InfoNotification(title, message);
377+
}
378+
367379
private async syncOnceInternal(syncConfig: CloudSyncConfig, fs: FileSystem) {
368380
this.logger.info("start sync once");
369381
// 获取文件列表
@@ -501,15 +513,7 @@ export class SynchronizeService {
501513
const rejected = syncResults.filter((ret) => ret.status === "rejected");
502514
if (rejected.length) {
503515
const hasConflict = rejected.some((ret) => isConflictError(ret.reason));
504-
this.logger.warn("skip status and digest update because cloud sync task failed", {
505-
conflict: hasConflict,
506-
failed: rejected.length,
507-
});
508-
const title = i18n.t("notification.script_sync_failed");
509-
const message = hasConflict
510-
? i18n.t("notification.script_sync_conflict_desc")
511-
: i18n.t("notification.script_sync_failed_desc");
512-
InfoNotification(title, message);
516+
this.notifySyncFailed(hasConflict, rejected.length);
513517
return;
514518
}
515519
// 同步状态

0 commit comments

Comments
 (0)