generated from vrerv/node-package-template
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add --timeout option to CLI #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sh1nj1
wants to merge
1
commit into
main
Choose a base branch
from
feature/add-timeout-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Test File for Manual Timeout Test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
jest.mock("@notionhq/client") // Ensure this is at the very top | ||
|
||
import { Client } from "@notionhq/client" // Import type, actual is mocked | ||
import { Command } from "commander" | ||
|
||
// Type alias for the mocked client, will be assigned in beforeEach | ||
let MockedClient: jest.MockedClass<typeof Client> | ||
|
||
// Mock 'readMarkdownFiles' and other functions to avoid side effects | ||
// These mocks are hoisted by Jest, so they apply to any subsequent require | ||
jest.mock("../src/index", () => ({ | ||
...jest.requireActual("../src/index"), | ||
readMarkdownFiles: jest.fn().mockReturnValue({ name: "mockDir", files: [], subfolders: [] }), | ||
syncToNotion: jest.fn().mockResolvedValue(undefined), | ||
printFolderHierarchy: jest.fn(), | ||
})) | ||
|
||
jest.mock("../src/sync-to-notion", () => ({ | ||
collectCurrentFiles: jest.fn().mockResolvedValue(new Map()), | ||
archiveChildPages: jest.fn().mockResolvedValue(undefined), | ||
})) | ||
|
||
describe("md-to-notion-cli", () => { | ||
let program: Command | ||
let originalArgv: string[] | ||
let consoleLogSpy: jest.SpyInstance | ||
let consoleErrorSpy: jest.SpyInstance | ||
let processExitSpy: jest.SpyInstance | ||
|
||
beforeEach(() => { | ||
jest.resetModules() // Reset modules first | ||
|
||
// Now require the mocked Client and the program | ||
MockedClient = require("@notionhq/client").Client as jest.MockedClass<typeof Client> | ||
program = require("../src/md-to-notion-cli").program | ||
|
||
// Clear mocks for each test | ||
MockedClient.mockClear() | ||
// jest.clearAllMocks() // This might be too broad if other mocks are needed across tests in a suite | ||
|
||
originalArgv = [...process.argv] | ||
process.argv = ["node", "md-to-notion-cli.js", "mockDir"] | ||
|
||
consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => {}) | ||
consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {}) | ||
processExitSpy = jest.spyOn(process, "exit").mockImplementation((() => {}) as (code?: string | number | null | undefined) => never) | ||
}) | ||
|
||
afterEach(() => { | ||
process.argv = originalArgv | ||
consoleLogSpy.mockRestore() | ||
consoleErrorSpy.mockRestore() | ||
processExitSpy.mockRestore() | ||
}) | ||
|
||
it("should call Notion Client with default timeout (10000ms) when --timeout is not provided", async () => { | ||
const testArgs = ["node", "md-to-notion-cli.js", "mockDir", "--token", "test-token", "--page-id", "test-page-id"] | ||
await program.parseAsync(testArgs, { from: "user" }) | ||
|
||
expect(MockedClient).toHaveBeenCalledTimes(1) | ||
expect(MockedClient).toHaveBeenCalledWith({ | ||
auth: "test-token", | ||
timeoutMs: 10000, | ||
}) | ||
}) | ||
|
||
it("should call Notion Client with specified timeout when --timeout is provided", async () => { | ||
const testArgs = ["node", "md-to-notion-cli.js", "mockDir", "--token", "test-token", "--page-id", "test-page-id", "--timeout", "5000"] | ||
await program.parseAsync(testArgs, { from: "user" }) | ||
|
||
expect(MockedClient).toHaveBeenCalledTimes(1) | ||
expect(MockedClient).toHaveBeenCalledWith({ | ||
auth: "test-token", | ||
timeoutMs: 5000, | ||
}) | ||
}) | ||
|
||
it("should call Notion Client with 1ms timeout when --timeout 1 is provided", async () => { | ||
const testArgs = ["node", "md-to-notion-cli.js", "mockDir", "--token", "test-token", "--page-id", "test-page-id", "--timeout", "1"] | ||
await program.parseAsync(testArgs, { from: "user" }) | ||
|
||
expect(MockedClient).toHaveBeenCalledTimes(1) | ||
expect(MockedClient).toHaveBeenCalledWith({ | ||
auth: "test-token", | ||
timeoutMs: 1, | ||
}) | ||
}) | ||
|
||
it("should parse timeout as an integer", async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider documenting that the timeout value is truncated to an integer when a non-integer input is provided, to clarify the behavior for future maintainers and users. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
const testArgs = ["node", "md-to-notion-cli.js", "mockDir", "--token", "test-token", "--page-id", "test-page-id", "--timeout", "123.45"] | ||
await program.parseAsync(testArgs, { from: "user" }) | ||
|
||
expect(MockedClient).toHaveBeenCalledTimes(1) | ||
expect(MockedClient).toHaveBeenCalledWith({ | ||
auth: "test-token", | ||
timeoutMs: 123, | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding validation to check if the result of parseInt is NaN and handle it appropriately, such as reverting to the default timeout or throwing a clear error.
Copilot uses AI. Check for mistakes.