Skip to content

Fix: Handle missing space information in Notion API response #1336

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

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions src/common/backend/services/notion/service.test.ts
Original file line number Diff line number Diff line change
@@ -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<IWebRequestService>;
let mockCookieService: jest.Mocked<ICookieService>;

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<string>
} 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<NotionUserContent> = {
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
});
4 changes: 4 additions & 0 deletions src/common/backend/services/notion/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions webpack/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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'),
Expand Down