diff --git a/src/backend/utils/SafeDriveService.ts b/src/backend/utils/SafeDriveService.ts index 90173ebd..9f0dc1a5 100644 --- a/src/backend/utils/SafeDriveService.ts +++ b/src/backend/utils/SafeDriveService.ts @@ -16,15 +16,15 @@ export type { } from "./SafeDriveService/SafeFilesCollection"; export class SafeDriveService_ { - public readonly Comments: SafeCommentsCollection_; - public readonly Drives: SafeDrivesCollection_; - public readonly Files: SafeFilesCollection_; + public readonly Comments: typeof SafeCommentsCollection_; + public readonly Drives: typeof SafeDrivesCollection_; + public readonly Files: typeof SafeFilesCollection_; public readonly Replies: GoogleAppsScript.Drive_v3.Drive.V3.Collection.RepliesCollection; public constructor() { - this.Comments = new SafeCommentsCollection_(); - this.Drives = new SafeDrivesCollection_(); - this.Files = new SafeFilesCollection_(); + this.Comments = SafeCommentsCollection_; + this.Drives = SafeDrivesCollection_; + this.Files = SafeFilesCollection_; this.Replies = Drive.Replies; } } diff --git a/src/backend/utils/SafeDriveService/SafeCommentsCollection.ts b/src/backend/utils/SafeDriveService/SafeCommentsCollection.ts index ec250ff7..f428abe0 100644 --- a/src/backend/utils/SafeDriveService/SafeCommentsCollection.ts +++ b/src/backend/utils/SafeDriveService/SafeCommentsCollection.ts @@ -20,77 +20,66 @@ interface SafeUser { me: boolean; } -export class SafeCommentsCollection_ { - private readonly unsafeComments: GoogleAppsScript.Drive_v3.Drive.V3.Collection.CommentsCollection; - - public constructor() { - // TODO: Remove and access directly - this.unsafeComments = Drive.Comments; - } - - private static commentIsSafe( - comment: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment, - ): comment is SafeComment { - return ( - comment.author !== undefined && - SafeCommentsCollection_.userIsSafe(comment.author) && - comment.id !== undefined && - comment.content !== undefined && - comment.replies?.every((reply) => - SafeCommentsCollection_.commentReplyIsSafe(reply), - ) === true - ); - } +function commentIsSafe_( + comment: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment, +): comment is SafeComment { + return ( + comment.author !== undefined && + userIsSafe_(comment.author) && + comment.id !== undefined && + comment.content !== undefined && + comment.replies?.every((reply) => commentReplyIsSafe_(reply)) === true + ); +} - private static commentListIsSafe( - commentList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList, - ): commentList is SafeCommentList { - return ( - commentList.comments?.every((comment) => - SafeCommentsCollection_.commentIsSafe(comment), - ) === true - ); - } +function commentListIsSafe_( + commentList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.CommentList, +): commentList is SafeCommentList { + return ( + commentList.comments?.every((comment) => commentIsSafe_(comment)) === true + ); +} - private static commentReplyIsSafe( - commentReply: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Reply, - ): commentReply is SafeReply { - return ( - commentReply.author !== undefined && - SafeCommentsCollection_.userIsSafe(commentReply.author) && - commentReply.content !== undefined - ); - } +function commentReplyIsSafe_( + commentReply: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Reply, +): commentReply is SafeReply { + return ( + commentReply.author !== undefined && + userIsSafe_(commentReply.author) && + commentReply.content !== undefined + ); +} - private static userIsSafe( - user: GoogleAppsScript.Drive_v3.Drive.V3.Schema.User, - ): user is SafeUser { - return user.me !== undefined && user.displayName !== undefined; - } +function userIsSafe_( + user: GoogleAppsScript.Drive_v3.Drive.V3.Schema.User, +): user is SafeUser { + return user.me !== undefined && user.displayName !== undefined; +} - public create( +export const SafeCommentsCollection_ = { + create: ( resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Comment, fileId: string, - ): SafeComment { - const ret = this.unsafeComments.create(resource, fileId); - if (!SafeCommentsCollection_.commentIsSafe(ret)) { + ): SafeComment => { + const ret = Drive.Comments.create(resource, fileId); + if (!commentIsSafe_(ret)) { throw new Error(""); } return ret; - } + }, - public list( + list: ( fileId: string, optionalArgs: { fields?: string; maxResults?: number; pageToken?: string | undefined; } = {}, - ): SafeCommentList { - const ret = this.unsafeComments.list(fileId, optionalArgs); - if (!SafeCommentsCollection_.commentListIsSafe(ret)) { + ): SafeCommentList => { + const ret = Drive.Comments.list(fileId, optionalArgs); + if (!commentListIsSafe_(ret)) { throw new Error(""); } return ret; - } -} + }, +}; diff --git a/src/backend/utils/SafeDriveService/SafeDrivesCollection.ts b/src/backend/utils/SafeDriveService/SafeDrivesCollection.ts index f51ea5ef..0aa259c5 100644 --- a/src/backend/utils/SafeDriveService/SafeDrivesCollection.ts +++ b/src/backend/utils/SafeDriveService/SafeDrivesCollection.ts @@ -18,60 +18,49 @@ const safeDriveKeys: DeepKeyof = { name: true, }; -export class SafeDrivesCollection_ { - private readonly unsafeDrives: GoogleAppsScript.Drive_v3.Drive.V3.Collection.DrivesCollection; - - public constructor() { - // TODO: Remove and access directly - this.unsafeDrives = Drive.Drives; +function driveIsSafe_>( + drive: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive, + keys: F | null, +): drive is typeof keys extends null ? SafeDrive : DeepPick { + if (keys === null) { + return driveIsSafe_(drive, safeDriveKeys); } - - private static driveIsSafe>( - drive: GoogleAppsScript.Drive_v3.Drive.V3.Schema.Drive, - keys: F | null, - ): drive is typeof keys extends null ? SafeDrive : DeepPick { - if (keys === null) { - return SafeDrivesCollection_.driveIsSafe(drive, safeDriveKeys); + for (const key in keys) { + if (!Object.prototype.hasOwnProperty.call(keys, key)) { + continue; } - for (const key in keys) { - if (!Object.prototype.hasOwnProperty.call(keys, key)) { - continue; - } - if (drive[key as keyof DeepKeyof] === undefined) { - return false; - } + if (drive[key as keyof DeepKeyof] === undefined) { + return false; } - return true; } + return true; +} - private static driveListIsSafe>( - driveList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList, - keys: F | null, - ): driveList is SafeDriveList { - return ( - driveList.drives?.every((file) => - SafeDrivesCollection_.driveIsSafe(file, keys), - ) === true - ); - } +function driveListIsSafe_>( + driveList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.DriveList, + keys: F | null, +): driveList is SafeDriveList { + return driveList.drives?.every((file) => driveIsSafe_(file, keys)) === true; +} - public list>( +export const SafeDrivesCollection_ = { + list: >( fields: F | null, optionalArgs: { maxResults?: number; orderBy?: string; pageToken?: string | undefined; } = {}, - ): SafeDriveList { - const ret = this.unsafeDrives.list({ + ): SafeDriveList => { + const ret = Drive.Drives.list({ ...optionalArgs, ...(fields !== null && { fields: `nextPageToken, drives(${stringifyFields_(fields)})`, }), }); - if (!SafeDrivesCollection_.driveListIsSafe(ret, fields)) { + if (!driveListIsSafe_(ret, fields)) { throw new Error(""); } return ret; - } -} + }, +}; diff --git a/src/backend/utils/SafeDriveService/SafeFilesCollection.ts b/src/backend/utils/SafeDriveService/SafeFilesCollection.ts index f106ea4e..51dd24da 100644 --- a/src/backend/utils/SafeDriveService/SafeFilesCollection.ts +++ b/src/backend/utils/SafeDriveService/SafeFilesCollection.ts @@ -51,120 +51,106 @@ type GetReturn, A extends GetArg> = A extends { ? string : DeepPick; -export class SafeFilesCollection_ { - private readonly unsafeFiles: GoogleAppsScript.Drive_v3.Drive.V3.Collection.FilesCollection; - - public constructor() { - // TODO: Remove and access directly - this.unsafeFiles = Drive.Files; +function fileIsSafe_>( + file: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, + keys: F | null, +): file is typeof keys extends null ? SafeFile : DeepPick { + if (keys === null) { + return fileIsSafe_(file, safeFileKeys); } - - private static fileIsSafe>( - file: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, - keys: F | null, - ): file is typeof keys extends null ? SafeFile : DeepPick { - if (keys === null) { - return SafeFilesCollection_.fileIsSafe(file, safeFileKeys); + for (const key in keys) { + if ( + !Object.prototype.hasOwnProperty.call(keys, key) || + safeFileOptionalKeys.indexOf(key) > -1 + ) { + continue; } - for (const key in keys) { - if ( - !Object.prototype.hasOwnProperty.call(keys, key) || - safeFileOptionalKeys.indexOf(key) > -1 - ) { - continue; - } - if (file[key as keyof DeepKeyof] === undefined) { - return false; - } - if (typeof keys[key] === "object") { - for (const innerKey in keys[key]) { - if ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- On purpose, we want to support arbitrary objects - (file[key as keyof DeepKeyof] as Record)[ - innerKey - ] === undefined - ) { - return false; - } + if (file[key as keyof DeepKeyof] === undefined) { + return false; + } + if (typeof keys[key] === "object") { + for (const innerKey in keys[key]) { + if ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- On purpose, we want to support arbitrary objects + (file[key as keyof DeepKeyof] as Record)[ + innerKey + ] === undefined + ) { + return false; } } } - return true; } + return true; +} - private static fileListIsSafe>( - fileList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.FileList, - keys: F | null, - ): fileList is SafeFileList { - return ( - fileList.files?.every((file) => - SafeFilesCollection_.fileIsSafe(file, keys), - ) === true - ); - } +function fileListIsSafe_>( + fileList: GoogleAppsScript.Drive_v3.Drive.V3.Schema.FileList, + keys: F | null, +): fileList is SafeFileList { + return fileList.files?.every((file) => fileIsSafe_(file, keys)) === true; +} - public copy>( +export const SafeFilesCollection_ = { + copy: >( resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, fileId: string, fields: F | null, optionalArgs: { supportsAllDrives?: boolean; } = {}, - ): DeepPick { - const ret = this.unsafeFiles.copy(resource, fileId, { + ): DeepPick => { + const ret = Drive.Files.copy(resource, fileId, { ...optionalArgs, ...(fields !== null && { fields: stringifyFields_(fields), }), }); - if (!SafeFilesCollection_.fileIsSafe(ret, fields)) { + if (!fileIsSafe_(ret, fields)) { throw new Error(""); } return ret; - } + }, - public create>( + create: >( resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, fields: F | null, mediaData?: GoogleAppsScript.Base.Blob, optionalArgs: { supportsAllDrives?: boolean; } = {}, - ): DeepPick { + ): DeepPick => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- Due to strange optional arguments on upstream types - const ret = this.unsafeFiles.create(resource, mediaData!, { + const ret = Drive.Files.create(resource, mediaData!, { ...optionalArgs, ...(fields !== null && { fields: stringifyFields_(fields), }), }); - if (!SafeFilesCollection_.fileIsSafe(ret, fields)) { + if (!fileIsSafe_(ret, fields)) { throw new Error(""); } return ret; - } + }, - public get, A extends GetArg>( + get: , A extends GetArg>( fileId: string, fields: F | null, optionalArgs: A = {} as A, - ): GetReturn { - const ret = this.unsafeFiles.get(fileId, { + ): GetReturn => { + const ret = Drive.Files.get(fileId, { ...optionalArgs, ...(fields !== null && { fields: stringifyFields_(fields), }), }); - if ( - typeof ret !== "string" && - !SafeFilesCollection_.fileIsSafe(ret, fields) - ) { + if (typeof ret !== "string" && !fileIsSafe_(ret, fields)) { throw new Error(""); } return ret as unknown as GetReturn; - } + }, - public list>( + list: >( fields: F | null, optionalArgs: { includeItemsFromAllDrives?: boolean; @@ -173,24 +159,24 @@ export class SafeFilesCollection_ { q?: string; supportsAllDrives?: boolean; } = {}, - ): SafeFileList { - const ret = this.unsafeFiles.list({ + ): SafeFileList => { + const ret = Drive.Files.list({ ...optionalArgs, ...(fields !== null && { fields: `nextPageToken, files(${stringifyFields_(fields)})`, }), }); - if (!SafeFilesCollection_.fileListIsSafe(ret, fields)) { + if (!fileListIsSafe_(ret, fields)) { throw new Error(""); } return ret; - } + }, - public remove(fileId: string): void { - this.unsafeFiles.remove(fileId); - } + remove: (fileId: string): void => { + Drive.Files.remove(fileId); + }, - public update>( + update: >( resource: GoogleAppsScript.Drive_v3.Drive.V3.Schema.File, fileId: string, fields: F | null, @@ -200,17 +186,17 @@ export class SafeFilesCollection_ { removeParents?: string; supportsAllDrives?: boolean; } = {}, - ): DeepPick { + ): DeepPick => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- Strange behaviour with optional arguments - const ret = this.unsafeFiles.update(resource, fileId, mediaData!, { + const ret = Drive.Files.update(resource, fileId, mediaData!, { ...optionalArgs, ...(fields !== null && { fields: stringifyFields_(fields), }), }); - if (!SafeFilesCollection_.fileIsSafe(ret, fields)) { + if (!fileIsSafe_(ret, fields)) { throw new Error(""); } return ret; - } -} + }, +}; diff --git a/tests/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts b/tests/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts index 84b0432b..f0939ddd 100644 --- a/tests/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts +++ b/tests/backend/utils/SafeDriveService/SafeCommentsCollection.test.ts @@ -3,14 +3,6 @@ import { expect, test, vi } from "vitest"; import { SafeCommentsCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeCommentsCollection"; import { mockedDrive } from "../../test-utils/gas-stubs"; -test("SafeCommentsCollection constructs correctly", () => { - global.Drive = mockedDrive(); - - expect(() => { - new SafeCommentsCollection_(); - }).not.toThrow(); -}); - test("create works", () => { const comment = { author: { @@ -35,9 +27,9 @@ test("create works", () => { .mocked(global.Drive.Comments) .create.mockReturnValueOnce(comment); - const commentsCollection = new SafeCommentsCollection_(); - - expect(commentsCollection.create(comment, "FILE_ID")).toStrictEqual(comment); + expect(SafeCommentsCollection_.create(comment, "FILE_ID")).toStrictEqual( + comment, + ); expect(create.mock.calls).toHaveLength(1); expect(create.mock.calls[0][0]).toBe(comment); @@ -67,9 +59,7 @@ test("create throws an error on an invalid comment", () => { .mocked(global.Drive.Comments) .create.mockReturnValueOnce(comment); - const commentsCollection = new SafeCommentsCollection_(); - - expect(() => commentsCollection.create(comment, "FILE_ID")).toThrow(""); + expect(() => SafeCommentsCollection_.create(comment, "FILE_ID")).toThrow(""); expect(create.mock.calls).toHaveLength(1); expect(create.mock.calls[0][0]).toBe(comment); @@ -121,9 +111,7 @@ test("list works", () => { .mocked(global.Drive.Comments) .list.mockReturnValueOnce(commentList); - const commentsCollection = new SafeCommentsCollection_(); - - expect(commentsCollection.list("FILE_ID")).toStrictEqual(commentList); + expect(SafeCommentsCollection_.list("FILE_ID")).toStrictEqual(commentList); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toBe("FILE_ID"); @@ -175,15 +163,13 @@ test("list works with optional arguments", () => { .mocked(global.Drive.Comments) .list.mockReturnValueOnce(commentList); - const commentsCollection = new SafeCommentsCollection_(); - const optionalArgs = { fields: "comments(id)", maxResults: 100, pageToken: "TOKEN", }; - expect(commentsCollection.list("FILE_ID", optionalArgs)).toStrictEqual( + expect(SafeCommentsCollection_.list("FILE_ID", optionalArgs)).toStrictEqual( commentList, ); @@ -236,9 +222,7 @@ test("list throws an error on an invalid comment", () => { .mocked(global.Drive.Comments) .list.mockReturnValueOnce(commentList); - const commentsCollection = new SafeCommentsCollection_(); - - expect(() => commentsCollection.list("FILE_ID")).toThrow(""); + expect(() => SafeCommentsCollection_.list("FILE_ID")).toThrow(""); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toBe("FILE_ID"); @@ -255,9 +239,7 @@ test("list throws an error on an invalid comment list", () => { .mocked(global.Drive.Comments) .list.mockReturnValueOnce(commentList); - const commentsCollection = new SafeCommentsCollection_(); - - expect(() => commentsCollection.list("FILE_ID")).toThrow(""); + expect(() => SafeCommentsCollection_.list("FILE_ID")).toThrow(""); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toBe("FILE_ID"); @@ -300,9 +282,7 @@ test("list throws an error on missing replies", () => { .mocked(global.Drive.Comments) .list.mockReturnValueOnce(commentList); - const commentsCollection = new SafeCommentsCollection_(); - - expect(() => commentsCollection.list("FILE_ID")).toThrow(""); + expect(() => SafeCommentsCollection_.list("FILE_ID")).toThrow(""); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toBe("FILE_ID"); @@ -353,9 +333,7 @@ test("list throws an error on an invalid reply", () => { .mocked(global.Drive.Comments) .list.mockReturnValueOnce(commentList); - const commentsCollection = new SafeCommentsCollection_(); - - expect(() => commentsCollection.list("FILE_ID")).toThrow(""); + expect(() => SafeCommentsCollection_.list("FILE_ID")).toThrow(""); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toBe("FILE_ID"); diff --git a/tests/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts b/tests/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts index 2831ed1a..a1dbbd35 100644 --- a/tests/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts +++ b/tests/backend/utils/SafeDriveService/SafeDrivesCollection.test.ts @@ -3,14 +3,6 @@ import { expect, test, vi } from "vitest"; import { SafeDrivesCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeDrivesCollection"; import { mockedDrive } from "../../test-utils/gas-stubs"; -test("SafeDrivesCollection constructs correctly", () => { - global.Drive = mockedDrive(); - - expect(() => { - new SafeDrivesCollection_(); - }).not.toThrow(); -}); - test("list works", () => { const driveList = { drives: [ @@ -30,9 +22,7 @@ test("list works", () => { .mocked(global.Drive.Drives) .list.mockReturnValueOnce(driveList); - const drivesCollection = new SafeDrivesCollection_(); - - expect(drivesCollection.list(null)).toStrictEqual(driveList); + expect(SafeDrivesCollection_.list(null)).toStrictEqual(driveList); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toStrictEqual({}); @@ -57,15 +47,15 @@ test("list works with optional parameters", () => { .mocked(global.Drive.Drives) .list.mockReturnValueOnce(driveList); - const drivesCollection = new SafeDrivesCollection_(); - const optionalArgs = { maxResults: 100, orderBy: "name", pageToken: "TOKEN", }; - expect(drivesCollection.list(null, optionalArgs)).toStrictEqual(driveList); + expect(SafeDrivesCollection_.list(null, optionalArgs)).toStrictEqual( + driveList, + ); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toStrictEqual(optionalArgs); @@ -101,10 +91,8 @@ test("list works with selective fields", () => { .list.mockReturnValueOnce(driveList1) .mockReturnValueOnce(driveList2); - const drivesCollection = new SafeDrivesCollection_(); - - expect(drivesCollection.list({ name: true })).toStrictEqual(driveList1); - expect(drivesCollection.list({ id: true, name: true })).toStrictEqual( + expect(SafeDrivesCollection_.list({ name: true })).toStrictEqual(driveList1); + expect(SafeDrivesCollection_.list({ id: true, name: true })).toStrictEqual( driveList2, ); @@ -134,9 +122,7 @@ test("list throws an error on an invalid drive", () => { .mocked(global.Drive.Drives) .list.mockReturnValueOnce(driveList); - const drivesCollection = new SafeDrivesCollection_(); - - expect(() => drivesCollection.list(null)).toThrow(""); + expect(() => SafeDrivesCollection_.list(null)).toThrow(""); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toStrictEqual({}); @@ -146,9 +132,7 @@ test("list throws an error on an invalid drive list", () => { global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Drives).list.mockReturnValueOnce({}); - const drivesCollection = new SafeDrivesCollection_(); - - expect(() => drivesCollection.list(null)).toThrow(""); + expect(() => SafeDrivesCollection_.list(null)).toThrow(""); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toStrictEqual({}); diff --git a/tests/backend/utils/SafeDriveService/SafeFilesCollection.test.ts b/tests/backend/utils/SafeDriveService/SafeFilesCollection.test.ts index fdbd6317..52660878 100644 --- a/tests/backend/utils/SafeDriveService/SafeFilesCollection.test.ts +++ b/tests/backend/utils/SafeDriveService/SafeFilesCollection.test.ts @@ -3,14 +3,6 @@ import { expect, test, vi } from "vitest"; import { SafeFilesCollection_ } from "../../../../src/backend/utils/SafeDriveService/SafeFilesCollection"; import { mockedDrive } from "../../test-utils/gas-stubs"; -test("SafeFilesCollection constructs correctly", () => { - global.Drive = mockedDrive(); - - expect(() => { - new SafeFilesCollection_(); - }).not.toThrow(); -}); - test("copy works", () => { const file = { capabilities: { @@ -25,9 +17,7 @@ test("copy works", () => { global.Drive = mockedDrive(); const copy = vi.mocked(global.Drive.Files).copy.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(filesCollection.copy(file, "TARGET_ID", null)).toBe(file); + expect(SafeFilesCollection_.copy(file, "TARGET_ID", null)).toBe(file); expect(copy.mock.calls).toHaveLength(1); expect(copy.mock.calls[0][0]).toBe(file); @@ -49,10 +39,10 @@ test("copy works with optional arguments", () => { global.Drive = mockedDrive(); const copy = vi.mocked(global.Drive.Files).copy.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - expect( - filesCollection.copy(file, "TARGET_ID", null, { supportsAllDrives: true }), + SafeFilesCollection_.copy(file, "TARGET_ID", null, { + supportsAllDrives: true, + }), ).toBe(file); expect(copy.mock.calls).toHaveLength(1); @@ -72,10 +62,8 @@ test("copy works with selective fields", () => { global.Drive = mockedDrive(); const copy = vi.mocked(global.Drive.Files).copy.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - expect( - filesCollection.copy( + SafeFilesCollection_.copy( file, "TARGET_ID", { capabilities: { canMoveItemOutOfDrive: true }, mimeType: true }, @@ -105,9 +93,7 @@ test("copy throws and error on invalid file", () => { global.Drive = mockedDrive(); const copy = vi.mocked(global.Drive.Files).copy.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(() => filesCollection.copy(file, "TARGET_ID", null)).toThrow(""); + expect(() => SafeFilesCollection_.copy(file, "TARGET_ID", null)).toThrow(""); expect(copy.mock.calls).toHaveLength(1); expect(copy.mock.calls[0][0]).toBe(file); @@ -129,9 +115,7 @@ test("get works", () => { global.Drive = mockedDrive(); const get = vi.mocked(global.Drive.Files).get.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(filesCollection.get("FILE_ID", null)).toBe(file); + expect(SafeFilesCollection_.get("FILE_ID", null)).toBe(file); expect(get.mock.calls).toHaveLength(1); expect(get.mock.calls[0][0]).toBe("FILE_ID"); @@ -152,9 +136,7 @@ test("get works with optional arguments", () => { global.Drive = mockedDrive(); const get = vi.mocked(global.Drive.Files).get.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(filesCollection.get("FILE_ID", null, { alt: "ALT" })).toBe(file); + expect(SafeFilesCollection_.get("FILE_ID", null, { alt: "ALT" })).toBe(file); expect(get.mock.calls).toHaveLength(1); expect(get.mock.calls[0][0]).toBe("FILE_ID"); @@ -170,9 +152,9 @@ test("get works with selective fields", () => { global.Drive = mockedDrive(); const get = vi.mocked(global.Drive.Files).get.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(filesCollection.get("FILE_ID", { id: true, name: true })).toBe(file); + expect(SafeFilesCollection_.get("FILE_ID", { id: true, name: true })).toBe( + file, + ); expect(get.mock.calls).toHaveLength(1); expect(get.mock.calls[0][0]).toBe("FILE_ID"); @@ -192,9 +174,7 @@ test("get throws an error on invalid file", () => { global.Drive = mockedDrive(); const get = vi.mocked(global.Drive.Files).get.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(() => filesCollection.get("FILE_ID", null)).toThrow(""); + expect(() => SafeFilesCollection_.get("FILE_ID", null)).toThrow(""); expect(get.mock.calls).toHaveLength(1); expect(get.mock.calls[0][0]).toBe("FILE_ID"); @@ -215,9 +195,7 @@ test("create works", () => { global.Drive = mockedDrive(); const create = vi.mocked(global.Drive.Files).create.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(filesCollection.create(file, null)).toBe(file); + expect(SafeFilesCollection_.create(file, null)).toBe(file); expect(create.mock.calls).toHaveLength(1); expect(create.mock.calls[0][0]).toBe(file); @@ -239,10 +217,10 @@ test("create works with optional arguments", () => { global.Drive = mockedDrive(); const create = vi.mocked(global.Drive.Files).create.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - expect( - filesCollection.create(file, null, undefined, { supportsAllDrives: true }), + SafeFilesCollection_.create(file, null, undefined, { + supportsAllDrives: true, + }), ).toBe(file); expect(create.mock.calls).toHaveLength(1); @@ -262,10 +240,8 @@ test("create works with selective fields", () => { global.Drive = mockedDrive(); const create = vi.mocked(global.Drive.Files).create.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - expect( - filesCollection.create(file, { + SafeFilesCollection_.create(file, { capabilities: { canDelete: true }, name: true, }), @@ -292,9 +268,7 @@ test("create throws an error on invalid file", () => { global.Drive = mockedDrive(); const create = vi.mocked(global.Drive.Files).create.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(() => filesCollection.create(file, null)).toThrow(""); + expect(() => SafeFilesCollection_.create(file, null)).toThrow(""); expect(create.mock.calls).toHaveLength(1); expect(create.mock.calls[0][0]).toBe(file); @@ -329,9 +303,7 @@ test("list works", () => { global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); - const filesCollection = new SafeFilesCollection_(); - - expect(filesCollection.list(null)).toBe(fileList); + expect(SafeFilesCollection_.list(null)).toBe(fileList); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toStrictEqual({}); @@ -364,8 +336,6 @@ test("list works with optional arguments", () => { global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); - const filesCollection = new SafeFilesCollection_(); - const optionalArgs = { includeItemsFromAllDrives: false, maxResults: 42, @@ -374,7 +344,7 @@ test("list works with optional arguments", () => { supportsAllDrives: true, }; - expect(filesCollection.list(null, optionalArgs)).toBe(fileList); + expect(SafeFilesCollection_.list(null, optionalArgs)).toBe(fileList); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toStrictEqual(optionalArgs); @@ -401,10 +371,8 @@ test("list works with selective fields", () => { global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); - const filesCollection = new SafeFilesCollection_(); - expect( - filesCollection.list({ + SafeFilesCollection_.list({ capabilities: { canMoveItemOutOfDrive: true }, id: true, }), @@ -440,9 +408,7 @@ test("list throws an error on invalid file", () => { global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); - const filesCollection = new SafeFilesCollection_(); - - expect(() => filesCollection.list(null)).toThrow(""); + expect(() => SafeFilesCollection_.list(null)).toThrow(""); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toStrictEqual({}); @@ -454,9 +420,7 @@ test("list throws an error on invalid file list", () => { global.Drive = mockedDrive(); const list = vi.mocked(global.Drive.Files).list.mockReturnValueOnce(fileList); - const filesCollection = new SafeFilesCollection_(); - - expect(() => filesCollection.list(null)).toThrow(""); + expect(() => SafeFilesCollection_.list(null)).toThrow(""); expect(list.mock.calls).toHaveLength(1); expect(list.mock.calls[0][0]).toStrictEqual({}); @@ -469,9 +433,7 @@ test("remove works", () => { () => {}, ); - const filesCollection = new SafeFilesCollection_(); - - filesCollection.remove("FILE_ID"); + SafeFilesCollection_.remove("FILE_ID"); expect(remove.mock.calls).toHaveLength(1); expect(remove.mock.calls[0][0]).toBe("FILE_ID"); @@ -491,9 +453,7 @@ test("update works", () => { global.Drive = mockedDrive(); const update = vi.mocked(global.Drive.Files).update.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(filesCollection.update(file, "FILE_ID", null)).toBe(file); + expect(SafeFilesCollection_.update(file, "FILE_ID", null)).toBe(file); expect(update.mock.calls).toHaveLength(1); expect(update.mock.calls[0][0]).toBe(file); @@ -516,8 +476,6 @@ test("update works with optional arguments", () => { global.Drive = mockedDrive(); const update = vi.mocked(global.Drive.Files).update.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - const optionalArgs = { addParents: "ADD_PARENT", removeParents: "REMOVE_PARENT", @@ -525,7 +483,7 @@ test("update works with optional arguments", () => { }; expect( - filesCollection.update(file, "FILE_ID", null, undefined, optionalArgs), + SafeFilesCollection_.update(file, "FILE_ID", null, undefined, optionalArgs), ).toBe(file); expect(update.mock.calls).toHaveLength(1); @@ -545,10 +503,8 @@ test("update works with selective fields", () => { global.Drive = mockedDrive(); const update = vi.mocked(global.Drive.Files).update.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - expect( - filesCollection.update(file, "FILE_ID", { + SafeFilesCollection_.update(file, "FILE_ID", { capabilities: { canMoveItemOutOfDrive: true }, }), ).toBe(file); @@ -570,9 +526,7 @@ test("update throws an error on invalid file", () => { global.Drive = mockedDrive(); const update = vi.mocked(global.Drive.Files).update.mockReturnValueOnce(file); - const filesCollection = new SafeFilesCollection_(); - - expect(() => filesCollection.update(file, "FILE_ID", null)).toThrow(""); + expect(() => SafeFilesCollection_.update(file, "FILE_ID", null)).toThrow(""); expect(update.mock.calls).toHaveLength(1); expect(update.mock.calls[0][0]).toBe(file);