diff --git a/package.json b/package.json index 0995cf3c..246d509e 100644 --- a/package.json +++ b/package.json @@ -97,7 +97,7 @@ "style-loader": "^0.23.1", "terser-webpack-plugin": "^2.3.1", "ts-loader": "^6.2.1", - "ts-node": "^10.2.0", + "ts-node": "^10.9.2", "typescript": "^5.1.6", "url-loader": "^3.0.0", "vitest": "^0.32.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7fb62266..ae8c2524 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -248,7 +248,7 @@ devDependencies: specifier: ^6.2.1 version: 6.2.2(typescript@5.5.4) ts-node: - specifier: ^10.2.0 + specifier: ^10.9.2 version: 10.9.2(@types/node@22.0.0)(typescript@5.5.4) typescript: specifier: ^5.1.6 @@ -4052,7 +4052,7 @@ packages: resolution: {integrity: sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==} engines: {node: '>= 4.0'} os: [darwin] - deprecated: The v1 package contains DANGEROUS / INSECURE binaries. Upgrade to safe fsevents v2 + deprecated: Upgrade to fsevents v2 to mitigate potential security issues requiresBuild: true dependencies: bindings: 1.5.0 diff --git a/src/common/backend/services/notion/service.test.ts b/src/common/backend/services/notion/service.test.ts new file mode 100644 index 00000000..b7d54e48 --- /dev/null +++ b/src/common/backend/services/notion/service.test.ts @@ -0,0 +1,83 @@ +import NotionDocumentService from './service'; +import { IWebRequestService } from '@/service/common/webRequest'; +import { ICookieService } from '@/service/common/cookie'; +import { NotionUserContent } from './types'; +import Container from 'typedi'; + +// Mock services +jest.mock('@/service/common/webRequest'); +jest.mock('@/service/common/cookie'); + +describe('NotionDocumentService', () => { + let notionService: NotionDocumentService; + let mockWebRequestService: jest.Mocked; + let mockCookieService: jest.Mocked; + + beforeEach(() => { + // Create new instances of mock services for each test + mockWebRequestService = { + startChangeHeader: jest.fn(), + end: jest.fn(), + changeUrl: jest.fn(url => Promise.resolve(url)), // Ensure changeUrl returns a Promise + } as any; // Using 'as any' to simplify mock structure for this example + mockCookieService = { + getAll: jest.fn().mockResolvedValue([]), // Mock getAll to return empty array or relevant cookie structure + set: jest.fn(), + remove: jest.fn(), + get: jest.fn(), + } as any; // Using 'as any' to simplify mock structure + + // Mock Container.get for each service + Container.get = jest.fn().mockImplementation((token: any) => { + if (token === IWebRequestService) { + return mockWebRequestService; + } + if (token === ICookieService) { + return mockCookieService; + } + throw new Error(`Unknown token: ${String(token)}`); + }) as any; + + notionService = new NotionDocumentService(); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + describe('getRepositories', () => { + it('should return an empty array when recordMap.space is undefined', async () => { + // Mock getUserContent to return a userContent object where recordMap.space is undefined + const mockUserContent: Partial = { + recordMap: { + // space is intentionally undefined + block: {}, + notion_user: {}, + collection: {}, + collection_view: {}, + comment: {}, + discussion: {}, + follow: {}, + space_view: {}, + user_root: {}, + user_settings: {}, + team: {}, + team_role: {}, + collection_block_column_order: {}, + collection_block_column_format: {}, + } as any, // Using 'as any' to bypass strict type checks for partial mock + }; + + jest.spyOn(notionService as any, 'getUserContent').mockResolvedValue(mockUserContent as NotionUserContent); + + const repositories = await notionService.getRepositories(); + + expect(repositories).toEqual([]); + expect((notionService as any).getUserContent).toHaveBeenCalledTimes(1); + }); + + // Add other test cases for getRepositories if needed + }); + + // Add tests for other methods of NotionDocumentService +}); diff --git a/src/common/backend/services/notion/service.ts b/src/common/backend/services/notion/service.ts index 37e933bc..9016a6eb 100644 --- a/src/common/backend/services/notion/service.ts +++ b/src/common/backend/services/notion/service.ts @@ -76,6 +76,10 @@ export default class NotionDocumentService implements DocumentService { this.userContent = await this.getUserContent(); } + if (!this.userContent.recordMap.space) { + return []; + } + const spaces = this.userContent.recordMap.space; const userId = Object.keys(this.userContent.recordMap.notion_user)[0] as string; diff --git a/webpack/webpack.common.js b/webpack/webpack.common.js index 2e8ccf58..85d46d83 100644 --- a/webpack/webpack.common.js +++ b/webpack/webpack.common.js @@ -12,7 +12,7 @@ function resolve(dir) { return path.join(__dirname, '..', dir); } -const distFiles = fs.readdirSync(resolve('dist')).filter((o) => o !== '.gitkeep'); +// const distFiles = fs.readdirSync(resolve('dist')).filter((o) => o !== '.gitkeep'); module.exports = { entry: { @@ -149,13 +149,13 @@ module.exports = { $: 'jquery', jQuery: 'jquery', }), - new CleanWebpackPlugin( - distFiles.map((p) => `dist/${p}`), - { - root: path.resolve(__dirname, '../'), - verbose: true, - } - ), + // new CleanWebpackPlugin( + // distFiles.map((p) => `dist/${p}`), + // { + // root: path.resolve(__dirname, '../'), + // verbose: true, + // } + // ), new CopyWebpackPlugin([ { from: resolve('chrome/html'),