import nock from 'nock';
export interface MockConfluencePage {
id: string;
title: string;
type: 'page' | 'blogpost';
status: 'current' | 'draft';
body?: {
storage?: {
value: string;
representation: 'storage';
};
view?: {
value: string;
representation: 'view';
};
};
version: {
number: number;
when: string;
};
space: {
key: string;
name: string;
};
_links: {
webui: string;
};
ancestors?: Array<{
id: string;
title: string;
}>;
excerpt?: string;
}
export class MockConfluenceAPI {
private scope: nock.Scope;
constructor(baseUrl: string = 'https://test-org.atlassian.net') {
this.scope = nock(baseUrl);
}
mockSearchPages(_query: string, results: MockConfluencePage[], totalSize: number = results.length) {
this.scope
.get('/wiki/rest/api/content/search')
.query(true) // Accept any query parameters
.reply(200, {
results,
size: totalSize,
limit: 25,
start: 0,
});
return this;
}
mockGetPageContent(pageId: string, page: MockConfluencePage) {
this.scope
.get(`/wiki/rest/api/content/${pageId}`)
.query(true)
.reply(200, page);
return this;
}
mockListSpacePages(spaceKey: string, results: MockConfluencePage[], totalSize: number = results.length) {
this.scope
.get('/wiki/rest/api/content')
.query((query) => query.spaceKey === spaceKey)
.reply(200, {
results,
size: totalSize,
limit: 25,
start: 0,
});
return this;
}
mockGetPageHierarchy(pageId: string, children: MockConfluencePage[]) {
this.scope
.get(`/wiki/rest/api/content/${pageId}/child/page`)
.query(true)
.reply(200, {
results: children,
size: children.length,
limit: 50,
start: 0,
});
return this;
}
mockGetPageByTitle(spaceKey: string, title: string, results: MockConfluencePage[]) {
this.scope
.get('/wiki/rest/api/content')
.query((query) => query.spaceKey === spaceKey && query.title === title)
.reply(200, {
results,
size: results.length,
limit: 25,
start: 0,
});
return this;
}
mockError(statusCode: number, message: string = 'Mock error') {
this.scope
.get(/\/wiki\/rest\/api\/.*/)
.query(true)
.reply(statusCode, {
message,
statusCode,
});
return this;
}
mockNotFound(path: string) {
this.scope
.get(path)
.query(true)
.reply(404, {
message: 'Page not found',
statusCode: 404,
});
return this;
}
done() {
return this.scope.done();
}
cleanup() {
nock.cleanAll();
}
}
export const createMockPage = (overrides: Partial<MockConfluencePage> = {}): MockConfluencePage => {
return {
id: '12345',
title: 'Test Page',
type: 'page',
status: 'current',
body: {
storage: {
value: '<p>Test page content</p>',
representation: 'storage',
},
view: {
value: '<p>Test page content</p>',
representation: 'view',
},
},
version: {
number: 1,
when: '2025-01-01T00:00:00.000Z',
},
space: {
key: 'TEST',
name: 'Test Space',
},
_links: {
webui: '/wiki/spaces/TEST/pages/12345/Test+Page',
},
excerpt: 'Test page excerpt',
...overrides,
};
};
export const createMockPages = (count: number, baseOverrides: Partial<MockConfluencePage> = {}): MockConfluencePage[] => {
return Array.from({ length: count }, (_, index) =>
createMockPage({
id: `${12345 + index}`,
title: `Test Page ${index + 1}`,
...baseOverrides,
})
);
};
export const waitForPromises = () => new Promise(setImmediate);