Skip to content

Commit

Permalink
Merge pull request #929 from skaut/resume-after-timeout
Browse files Browse the repository at this point in the history
Backend: Added the ability to recover from a timeout
  • Loading branch information
marekdedic authored May 21, 2022
2 parents 9b99856 + 93a2a4f commit ff592df
Show file tree
Hide file tree
Showing 23 changed files with 2,548 additions and 885 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ module.exports = {
"jest/prefer-called-with": "error",
"jest/prefer-expect-assertions": [
"error",
{ onlyFunctionsWithAsyncKeyword: true },
{
onlyFunctionsWithAsyncKeyword: true,
onlyFunctionsWithExpectInLoop: true,
onlyFunctionsWithExpectInCallback: true,
},
],
"jest/prefer-expect-resolves": "error",
"jest/prefer-hooks-on-top": "error",
Expand Down
490 changes: 372 additions & 118 deletions __tests__/backend/move.test.ts

Large diffs are not rendered by default.

109 changes: 51 additions & 58 deletions __tests__/backend/move/moveFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { mocked } from "jest-mock";

import { MoveContext_ } from "../../../src/backend/utils/MoveContext";
import { moveFile_ } from "../../../src/backend/move/moveFile";

import { ErrorLogger_ } from "../../../src/backend/utils/ErrorLogger";
import { MoveState_ } from "../../../src/backend/utils/MoveState";
import * as copyFileComments from "../../../src/backend/move/copyFileComments";
import { mockedDrive, mockedFilesCollection } from "../../test-utils/gas-stubs";

jest.mock("../../../src/backend/utils/ErrorLogger");
jest.mock("../../../src/backend/utils/MoveState");
jest.mock("../../../src/backend/move/copyFileComments");

test("moveFile works correctly with a file that can be moved directly", () => {
Expand Down Expand Up @@ -35,20 +34,20 @@ test("moveFile works correctly with a file that can be moved directly", () => {
update,
},
};
const logger = new ErrorLogger_();
const state = new MoveState_("SRC_BASE_ID", "DEST_BASE_ID", false, false);

moveFile_(
{
capabilities: { canMoveItemOutOfDrive: true },
id: "SRC_FILE_ID",
title: "FILE_NAME",
},
new MoveContext_(
"SRC_PARENT_ID",
"DEST_PARENT_ID",
["PATH", "TO", "FILE"],
logger
),
state,
{
sourceID: "SRC_PARENT_ID",
destinationID: "DEST_PARENT_ID",
path: ["PATH", "TO", "FILE"],
},
false
);

Expand All @@ -58,7 +57,6 @@ test("moveFile works correctly with a file that can be moved directly", () => {
expect(update.mock.calls[0][3]!.addParents).toContain("DEST_PARENT_ID");
expect(update.mock.calls[0][3]!.removeParents).toContain("SRC_PARENT_ID");
expect(update.mock.calls[0][3]!.supportsAllDrives).toBe(true);
expect(mocked(logger).log.mock.calls).toHaveLength(0);
});

test("moveFile works correctly with a file that can be moved out of drive, yet cannot be moved directly", () => {
Expand Down Expand Up @@ -103,20 +101,21 @@ test("moveFile works correctly with a file that can be moved out of drive, yet c
update,
},
};
const logger = new ErrorLogger_();
const state = new MoveState_("SRC_BASE_ID", "DEST_BASE_ID", false, false);
mocked(state).tryOrLog.mockImplementation((_, fn) => fn());

moveFile_(
{
capabilities: { canMoveItemOutOfDrive: true },
id: "SRC_FILE_ID",
title: "FILE_NAME",
},
new MoveContext_(
"SRC_PARENT_ID",
"DEST_PARENT_ID",
["PATH", "TO", "FILE"],
logger
),
state,
{
sourceID: "SRC_PARENT_ID",
destinationID: "DEST_PARENT_ID",
path: ["PATH", "TO", "FILE"],
},
false
);

Expand All @@ -133,7 +132,6 @@ test("moveFile works correctly with a file that can be moved out of drive, yet c
expect(copy.mock.calls[0][1]).toBe("SRC_FILE_ID");
expect(copy.mock.calls[0][2]).toBeDefined();
expect(copy.mock.calls[0][2]!.supportsAllDrives).toBe(true);
expect(mocked(logger).log.mock.calls).toHaveLength(0);
});

test("moveFile works correctly with a file that cannot be moved out of drive", () => {
Expand All @@ -159,20 +157,21 @@ test("moveFile works correctly with a file that cannot be moved out of drive", (
copy,
},
};
const logger = new ErrorLogger_();
const state = new MoveState_("SRC_BASE_ID", "DEST_BASE_ID", false, false);
mocked(state).tryOrLog.mockImplementation((_, fn) => fn());

moveFile_(
{
capabilities: { canMoveItemOutOfDrive: false },
id: "SRC_FILE_ID",
title: "FILE_NAME",
},
new MoveContext_(
"SRC_PARENT_ID",
"DEST_PARENT_ID",
["PATH", "TO", "FILE"],
logger
),
state,
{
sourceID: "SRC_PARENT_ID",
destinationID: "DEST_PARENT_ID",
path: ["PATH", "TO", "FILE"],
},
false
);

Expand All @@ -183,7 +182,6 @@ test("moveFile works correctly with a file that cannot be moved out of drive", (
expect(copy.mock.calls[0][1]).toBe("SRC_FILE_ID");
expect(copy.mock.calls[0][2]).toBeDefined();
expect(copy.mock.calls[0][2]!.supportsAllDrives).toBe(true);
expect(mocked(logger).log.mock.calls).toHaveLength(0);
});

test("moveFile works correctly with a file that can be moved directly with comments", () => {
Expand Down Expand Up @@ -211,20 +209,20 @@ test("moveFile works correctly with a file that can be moved directly with comme
update,
},
};
const logger = new ErrorLogger_();
const state = new MoveState_("SRC_BASE_ID", "DEST_BASE_ID", true, false);

moveFile_(
{
capabilities: { canMoveItemOutOfDrive: true },
id: "SRC_FILE_ID",
title: "FILE_NAME",
},
new MoveContext_(
"SRC_PARENT_ID",
"DEST_PARENT_ID",
["PATH", "TO", "FILE"],
logger
),
state,
{
sourceID: "SRC_PARENT_ID",
destinationID: "DEST_PARENT_ID",
path: ["PATH", "TO", "FILE"],
},
true
);

Expand All @@ -234,7 +232,6 @@ test("moveFile works correctly with a file that can be moved directly with comme
expect(update.mock.calls[0][3]!.addParents).toContain("DEST_PARENT_ID");
expect(update.mock.calls[0][3]!.removeParents).toContain("SRC_PARENT_ID");
expect(update.mock.calls[0][3]!.supportsAllDrives).toBe(true);
expect(mocked(logger).log.mock.calls).toHaveLength(0);
});

test("moveFile works correctly with a file that cannot be moved out of drive with comments", () => {
Expand All @@ -261,20 +258,21 @@ test("moveFile works correctly with a file that cannot be moved out of drive wit
},
};
mocked(copyFileComments).copyFileComments_.mockReturnValueOnce();
const logger = new ErrorLogger_();
const state = new MoveState_("SRC_BASE_ID", "DEST_BASE_ID", true, false);
mocked(state).tryOrLog.mockImplementation((_, fn) => fn());

moveFile_(
{
capabilities: { canMoveItemOutOfDrive: false },
id: "SRC_FILE_ID",
title: "FILE_NAME",
},
new MoveContext_(
"SRC_PARENT_ID",
"DEST_PARENT_ID",
["PATH", "TO", "FILE"],
logger
),
state,
{
sourceID: "SRC_PARENT_ID",
destinationID: "DEST_PARENT_ID",
path: ["PATH", "TO", "FILE"],
},
true
);

Expand All @@ -292,10 +290,10 @@ test("moveFile works correctly with a file that cannot be moved out of drive wit
expect(mocked(copyFileComments).copyFileComments_.mock.calls[0][1]).toBe(
"DEST_FILE_ID"
);
expect(mocked(logger).log.mock.calls).toHaveLength(0);
});

test("moveFile fails gracefully on error", () => {
expect.assertions(1);
interface CopyFileOptions {
supportsAllDrives?: boolean;
fields?: string;
Expand Down Expand Up @@ -339,28 +337,23 @@ test("moveFile fails gracefully on error", () => {
update,
},
};
const logger = new ErrorLogger_();
const state = new MoveState_("SRC_BASE_ID", "DEST_BASE_ID", false, false);
mocked(state).tryOrLog.mockImplementation((_, fn) => {
expect(fn).toThrow("ERROR_MESAGE");
});

moveFile_(
{
capabilities: { canMoveItemOutOfDrive: true },
id: "SRC_FILE_ID",
title: "FILE_NAME",
},
new MoveContext_(
"SRC_PARENT_ID",
"DEST_PARENT_ID",
["PATH", "TO", "FILE"],
logger
),
state,
{
sourceID: "SRC_PARENT_ID",
destinationID: "DEST_PARENT_ID",
path: ["PATH", "TO", "FILE"],
},
false
);
expect(mocked(logger).log.mock.calls).toHaveLength(1);
expect(mocked(logger).log.mock.calls[0][0]).toStrictEqual([
"PATH",
"TO",
"FILE",
"FILE_NAME",
]);
expect(mocked(logger).log.mock.calls[0][1]).not.toBe("");
});
Loading

0 comments on commit ff592df

Please sign in to comment.