diff --git a/tests/backend/test-utils/gas-stubs.ts b/tests/backend/test-utils/gas-stubs.ts index 70c43e9b..0a236b8a 100644 --- a/tests/backend/test-utils/gas-stubs.ts +++ b/tests/backend/test-utils/gas-stubs.ts @@ -1,38 +1,6 @@ import { vi } from "vitest"; -export function mockedCommentsCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.CommentsCollection { - return { - create: - vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment, - fileId: string, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment - >(), - get: vi.fn< - ( - fileId: string, - commentId: string, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment - >(), - list: vi.fn< - ( - fileId: string, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList - >(), - remove: vi.fn<(fileId: string, commentId: string) => void>(), - update: - vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment, - fileId: string, - commentId: string, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment - >(), - }; -} +/* eslint-disable @typescript-eslint/no-explicit-any -- These are stubs for external functions */ export function mockedDrive(): GoogleAppsScript.Drive { return { @@ -149,129 +117,6 @@ export function mockedDrive(): GoogleAppsScript.Drive { }; } -export function mockedDrivesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.DrivesCollection { - return { - create: - vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive, - requestId: string, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive - >(), - get: vi.fn< - ( - driveId: string, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive - >(), - hide: vi.fn< - (driveId: string) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive - >(), - list: vi.fn< - ( - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList - >(), - remove: - vi.fn<(driveId: string, optionalArgs?: Record) => void>(), - unhide: - vi.fn< - (driveId: string) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive - >(), - update: - vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive, - driveId: string, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive - >(), - }; -} - -export function mockedFilesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.FilesCollection { - return { - copy: vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, - fileId: string, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File - >(), - create: - vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, - mediaData?: GoogleAppsScript.Base.Blob, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File - >(), - download: - vi.fn< - ( - fileId: string, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Operation - >(), - emptyTrash: vi.fn<(optionalArgs?: Record) => void>(), - export: vi.fn<(fileId: string, mimeType: string) => void>(), - generateIds: - vi.fn< - ( - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.GeneratedIds - >(), - get: vi.fn() as { - ( - fileId: string, - optionalArgs?: Record & { alt: "media" }, - ): string; - ( - fileId: string, - optionalArgs?: Record, - ): GoogleAppsScript.Drive_v3.Drive.V3.Schema.File; - }, - list: vi.fn< - ( - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.FileList - >(), - listLabels: - vi.fn< - ( - fileId: string, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.LabelList - >(), - modifyLabels: - vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.ModifyLabelsRequest, - fileId: string, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.ModifyLabelsResponse - >(), - remove: - vi.fn<(fileId: string, optionalArgs?: Record) => void>(), - update: - vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, - fileId: string, - mediaData?: GoogleAppsScript.Base.Blob, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File - >(), - watch: - vi.fn< - ( - resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Channel, - fileId: string, - optionalArgs?: Record, - ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Channel - >(), - }; -} - export function mockedHtmlOutput(): GoogleAppsScript.HTML.HtmlOutput { return { addMetaTag: @@ -602,6 +447,163 @@ function mockedChannelsCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collecti }; } +function mockedCommentsCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.CommentsCollection { + return { + create: + vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment, + fileId: string, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment + >(), + get: vi.fn< + ( + fileId: string, + commentId: string, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment + >(), + list: vi.fn< + ( + fileId: string, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList + >(), + remove: vi.fn<(fileId: string, commentId: string) => void>(), + update: + vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment, + fileId: string, + commentId: string, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment + >(), + }; +} + +function mockedDrivesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.DrivesCollection { + return { + create: + vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive, + requestId: string, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive + >(), + get: vi.fn< + ( + driveId: string, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive + >(), + hide: vi.fn< + (driveId: string) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive + >(), + list: vi.fn< + ( + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList + >(), + remove: + vi.fn<(driveId: string, optionalArgs?: Record) => void>(), + unhide: + vi.fn< + (driveId: string) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive + >(), + update: + vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive, + driveId: string, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive + >(), + }; +} + +function mockedFilesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.FilesCollection { + return { + copy: vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, + fileId: string, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File + >(), + create: + vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, + mediaData?: GoogleAppsScript.Base.Blob, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File + >(), + download: + vi.fn< + ( + fileId: string, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Operation + >(), + emptyTrash: vi.fn<(optionalArgs?: Record) => void>(), + export: vi.fn<(fileId: string, mimeType: string) => void>(), + generateIds: + vi.fn< + ( + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.GeneratedIds + >(), + get: vi.fn() as { + ( + fileId: string, + optionalArgs?: Record & { alt: "media" }, + ): string; + ( + fileId: string, + optionalArgs?: Record, + ): GoogleAppsScript.Drive_v3.Drive.V3.Schema.File; + }, + list: vi.fn< + ( + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.FileList + >(), + listLabels: + vi.fn< + ( + fileId: string, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.LabelList + >(), + modifyLabels: + vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.ModifyLabelsRequest, + fileId: string, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.ModifyLabelsResponse + >(), + remove: + vi.fn<(fileId: string, optionalArgs?: Record) => void>(), + update: + vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, + fileId: string, + mediaData?: GoogleAppsScript.Base.Blob, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.File + >(), + watch: + vi.fn< + ( + resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Channel, + fileId: string, + optionalArgs?: Record, + ) => GoogleAppsScript.Drive_v3.Drive.V3.Schema.Channel + >(), + }; +} + function mockedOperationCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collection.OperationCollection { return { cancel: vi.fn<(name: string) => void>(), @@ -723,3 +725,5 @@ function mockedTeamdrivesCollection(): GoogleAppsScript.Drive_v3.Drive.V3.Collec >(), }; } + +/* eslint-enable */ diff --git a/tests/backend/utils/SafeDriveService.test.ts b/tests/backend/utils/SafeDriveService.test.ts index 04e4995c..d030442b 100644 --- a/tests/backend/utils/SafeDriveService.test.ts +++ b/tests/backend/utils/SafeDriveService.test.ts @@ -1,22 +1,10 @@ import { expect, test } from "vitest"; import { SafeDriveService_ } from "../../../src/backend/utils/SafeDriveService"; -import { - mockedCommentsCollection, - mockedDrive, - mockedDrivesCollection, - mockedFilesCollection, - mockedRepliesCollection, -} from "../test-utils/gas-stubs"; +import { mockedDrive } from "../test-utils/gas-stubs"; test("SafeDriveService constructs correctly", () => { - global.Drive = { - ...mockedDrive(), - Comments: mockedCommentsCollection(), - Drives: mockedDrivesCollection(), - Files: mockedFilesCollection(), - Replies: mockedRepliesCollection(), - }; + global.Drive = mockedDrive(); expect(() => { new SafeDriveService_(); diff --git a/tests/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts b/tests/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts index 35791ffe..84b0432b 100644 --- a/tests/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts +++ b/tests/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts @@ -1,16 +1,10 @@ import { expect, test, vi } from "vitest"; import { SafeCommentsCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeCommentsCollection"; -import { - mockedCommentsCollection, - mockedDrive, -} from "../../test-utils/gas-stubs"; +import { mockedDrive } from "../../test-utils/gas-stubs"; test("SafeCommentsCollection constructs correctly", () => { - global.Drive = { - ...mockedDrive(), - Comments: mockedCommentsCollection(), - }; + global.Drive = mockedDrive(); expect(() => { new SafeCommentsCollection_(); diff --git a/tests/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts b/tests/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts index 91009210..2831ed1a 100644 --- a/tests/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts +++ b/tests/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts @@ -1,16 +1,10 @@ import { expect, test, vi } from "vitest"; import { SafeDrivesCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeDrivesCollection"; -import { - mockedDrive, - mockedDrivesCollection, -} from "../../test-utils/gas-stubs"; +import { mockedDrive } from "../../test-utils/gas-stubs"; test("SafeDrivesCollection constructs correctly", () => { - global.Drive = { - ...mockedDrive(), - Drives: mockedDrivesCollection(), - }; + global.Drive = mockedDrive(); expect(() => { new SafeDrivesCollection_(); @@ -149,7 +143,7 @@ test("list throws an error on an invalid drive", () => { }); test("list throws an error on an invalid drive list", () => { - global.Drive.Drives = mockedDrivesCollection(); + global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Drives).list.mockReturnValueOnce({}); const drivesCollection = new SafeDrivesCollection_(); diff --git a/tests/backend/utils/SafeDriveService/SafeFilesCollection.test.ts b/tests/backend/utils/SafeDriveService/SafeFilesCollection.test.ts index 2dd1153d..fdbd6317 100644 --- a/tests/backend/utils/SafeDriveService/SafeFilesCollection.test.ts +++ b/tests/backend/utils/SafeDriveService/SafeFilesCollection.test.ts @@ -1,13 +1,10 @@ import { expect, test, vi } from "vitest"; import { SafeFilesCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeFilesCollection"; -import { mockedDrive, mockedFilesCollection } from "../../test-utils/gas-stubs"; +import { mockedDrive } from "../../test-utils/gas-stubs"; test("SafeFilesCollection constructs correctly", () => { - global.Drive = { - ...mockedDrive(), - Files: mockedFilesCollection(), - }; + global.Drive = mockedDrive(); expect(() => { new SafeFilesCollection_(); @@ -25,7 +22,7 @@ test("copy works", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const copy = vi.mocked(global.Drive.Files).copy.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -49,7 +46,7 @@ test("copy works with optional arguments", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const copy = vi.mocked(global.Drive.Files).copy.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -72,7 +69,7 @@ test("copy works with selective fields", () => { mimeType: "text/plain", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const copy = vi.mocked(global.Drive.Files).copy.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -105,7 +102,7 @@ test("copy throws and error on invalid file", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const copy = vi.mocked(global.Drive.Files).copy.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -129,7 +126,7 @@ test("get works", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const get = vi.mocked(global.Drive.Files).get.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -152,7 +149,7 @@ test("get works with optional arguments", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const get = vi.mocked(global.Drive.Files).get.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -170,7 +167,7 @@ test("get works with selective fields", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const get = vi.mocked(global.Drive.Files).get.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -192,7 +189,7 @@ test("get throws an error on invalid file", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const get = vi.mocked(global.Drive.Files).get.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -215,7 +212,7 @@ test("create works", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const create = vi.mocked(global.Drive.Files).create.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -239,7 +236,7 @@ test("create works with optional arguments", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const create = vi.mocked(global.Drive.Files).create.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -262,7 +259,7 @@ test("create works with selective fields", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const create = vi.mocked(global.Drive.Files).create.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -292,7 +289,7 @@ test("create throws an error on invalid file", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const create = vi.mocked(global.Drive.Files).create.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -329,7 +326,7 @@ test("list works", () => { ], }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -364,7 +361,7 @@ test("list works with optional arguments", () => { ], }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -401,7 +398,7 @@ test("list works with selective fields", () => { ], }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -440,7 +437,7 @@ test("list throws an error on invalid file", () => { ], }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -454,7 +451,7 @@ test("list throws an error on invalid file", () => { test("list throws an error on invalid file list", () => { const fileList = {}; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); const filesCollection = new SafeFilesCollection_(); @@ -466,7 +463,7 @@ test("list throws an error on invalid file list", () => { }); test("remove works", () => { - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const remove = vi.mocked(global.Drive.Files).remove.mockImplementationOnce( // eslint-disable-next-line @typescript-eslint/no-empty-function -- Implementation needed so the function is bound to local this () => {}, @@ -491,7 +488,7 @@ test("update works", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const update = vi.mocked(global.Drive.Files).update.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -516,7 +513,7 @@ test("update works with optional arguments", () => { name: "FILE_TITLE", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const update = vi.mocked(global.Drive.Files).update.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -545,7 +542,7 @@ test("update works with selective fields", () => { }, }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const update = vi.mocked(global.Drive.Files).update.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_(); @@ -570,7 +567,7 @@ test("update throws an error on invalid file", () => { id: "FILE_ID", }; - global.Drive.Files = mockedFilesCollection(); + global.Drive = mockedDrive(); const update = vi.mocked(global.Drive.Files).update.mockReturnValueOnce(file); const filesCollection = new SafeFilesCollection_();