Skip to content

Commit a02a83e

Browse files
google-labs-jules[bot]DiamondYuan
authored andcommitted
Fix: Handle missing space information in Notion API response
The /api/v3/loadUserContent endpoint for Notion may not return space information. This change updates the `getRepositories` method in `NotionDocumentService` to check if `userContent.recordMap.space` is undefined. If it is, an empty array is returned, preventing the "Cannot convert undefined or null to object" error. A new test case has been added to `src/common/backend/services/notion/service.test.ts` to verify this behavior. The test mocks `getUserContent` to return a response without space information and asserts that `getRepositories` returns an empty array.
1 parent b0aee65 commit a02a83e

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import NotionDocumentService from './service';
2+
import { IWebRequestService } from '@/service/common/webRequest';
3+
import { ICookieService } from '@/service/common/cookie';
4+
import { NotionUserContent } from './types';
5+
import Container from 'typedi';
6+
7+
// Mock services
8+
jest.mock('@/service/common/webRequest');
9+
jest.mock('@/service/common/cookie');
10+
11+
describe('NotionDocumentService', () => {
12+
let notionService: NotionDocumentService;
13+
let mockWebRequestService: jest.Mocked<IWebRequestService>;
14+
let mockCookieService: jest.Mocked<ICookieService>;
15+
16+
beforeEach(() => {
17+
// Create new instances of mock services for each test
18+
mockWebRequestService = {
19+
startChangeHeader: jest.fn(),
20+
end: jest.fn(),
21+
changeUrl: jest.fn(url => Promise.resolve(url)), // Ensure changeUrl returns a Promise<string>
22+
} as any; // Using 'as any' to simplify mock structure for this example
23+
mockCookieService = {
24+
getAll: jest.fn().mockResolvedValue([]), // Mock getAll to return empty array or relevant cookie structure
25+
set: jest.fn(),
26+
remove: jest.fn(),
27+
get: jest.fn(),
28+
} as any; // Using 'as any' to simplify mock structure
29+
30+
// Mock Container.get for each service
31+
Container.get = jest.fn().mockImplementation((token: any) => {
32+
if (token === IWebRequestService) {
33+
return mockWebRequestService;
34+
}
35+
if (token === ICookieService) {
36+
return mockCookieService;
37+
}
38+
throw new Error(`Unknown token: ${String(token)}`);
39+
}) as any;
40+
41+
notionService = new NotionDocumentService();
42+
});
43+
44+
afterEach(() => {
45+
jest.clearAllMocks();
46+
});
47+
48+
describe('getRepositories', () => {
49+
it('should return an empty array when recordMap.space is undefined', async () => {
50+
// Mock getUserContent to return a userContent object where recordMap.space is undefined
51+
const mockUserContent: Partial<NotionUserContent> = {
52+
recordMap: {
53+
// space is intentionally undefined
54+
block: {},
55+
notion_user: {},
56+
collection: {},
57+
collection_view: {},
58+
comment: {},
59+
discussion: {},
60+
follow: {},
61+
space_view: {},
62+
user_root: {},
63+
user_settings: {},
64+
team: {},
65+
team_role: {},
66+
collection_block_column_order: {},
67+
collection_block_column_format: {},
68+
} as any, // Using 'as any' to bypass strict type checks for partial mock
69+
};
70+
71+
jest.spyOn(notionService as any, 'getUserContent').mockResolvedValue(mockUserContent as NotionUserContent);
72+
73+
const repositories = await notionService.getRepositories();
74+
75+
expect(repositories).toEqual([]);
76+
expect((notionService as any).getUserContent).toHaveBeenCalledTimes(1);
77+
});
78+
79+
// Add other test cases for getRepositories if needed
80+
});
81+
82+
// Add tests for other methods of NotionDocumentService
83+
});

src/common/backend/services/notion/service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export default class NotionDocumentService implements DocumentService {
7676
this.userContent = await this.getUserContent();
7777
}
7878

79+
if (!this.userContent.recordMap.space) {
80+
return [];
81+
}
82+
7983
const spaces = this.userContent.recordMap.space;
8084

8185
const userId = Object.keys(this.userContent.recordMap.notion_user)[0] as string;

0 commit comments

Comments
 (0)