-
Notifications
You must be signed in to change notification settings - Fork 31
/
Publisher.test.ts
311 lines (295 loc) · 8.34 KB
/
Publisher.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* eslint-disable @typescript-eslint/naming-convention */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { expect, test } from "@jest/globals";
import { ConfluenceClient } from "confluence.js";
import {
BinaryFile,
FilesToUpload,
LoaderAdaptor,
MarkdownFile,
} from "./adaptors";
import { orderMarks } from "./AdfEqual";
import { ConfluencePerPageAllValues } from "./ConniePageConfig";
import { ChartData, MermaidRenderer } from "./mermaid_renderers";
import { Publisher } from "./Publisher";
import {
AutoSettingsLoader,
DefaultSettingsLoader,
EnvironmentVariableSettingsLoader,
StaticSettingsLoader,
} from "./Settings";
const settingsLoader = new AutoSettingsLoader();
const settings = settingsLoader.load();
const markdownTestCases: MarkdownFile[] = [
{
folderName: "headers",
absoluteFilePath: "/path/to/headers.md",
fileName: "headers.md",
contents:
"# Header 1\n\n## Header 2\n\n### Header 3\n\n#### Header 4\n\n##### Header 5\n\n###### Header 6",
pageTitle: "Headers",
frontmatter: {
title: "Headers",
description:
"A Markdown file demonstrating different header levels.",
},
},
{
folderName: "emphasis",
absoluteFilePath: "/path/to/emphasis.md",
fileName: "emphasis.md",
contents:
"*Italic text*\n\n_Italic text_\n\n**Bold text**\n\n__Bold text__\n\n***Bold and italic text***\n\n___Bold and italic text___",
pageTitle: "Emphasis",
frontmatter: {
title: "Emphasis",
description:
"A Markdown file demonstrating different text emphasis styles.",
},
},
{
folderName: "lists",
absoluteFilePath: "/path/to/lists.md",
fileName: "lists.md",
contents:
"1. First ordered list item\n2. Second ordered list item\n\n- Unordered list item\n- Another unordered list item",
pageTitle: "Lists",
frontmatter: {
title: "Lists",
description:
"A Markdown file demonstrating ordered and unordered lists.",
},
},
{
folderName: "links",
absoluteFilePath: "/path/to/links.md",
fileName: "links.md",
contents:
'[Example link](https://example.com)\n\n[Example link with title](https://example.com "Example Title")',
pageTitle: "Links",
frontmatter: {
title: "Links",
description: "A Markdown file demonstrating different link styles.",
},
},
/*
{
folderName: "images",
absoluteFilePath: "/path/to/images.md",
fileName: "images.md",
contents:
'![Alt text](/path/to/image.jpg)\n\n![Alt text with title](/path/to/image.jpg "Image Title")',
pageTitle: "Images",
frontmatter: {
title: "Images",
description:
"A Markdown file demonstrating different image styles.",
},
},
*/
{
folderName: "code",
absoluteFilePath: "/path/to/code.md",
fileName: "code.md",
contents: "Inline `code` example\n\n```\nCode block example\n```",
pageTitle: "Code",
frontmatter: {
title: "Code",
description:
"A Markdown file demonstrating inline code and code blocks.",
},
},
{
folderName: "tables",
absoluteFilePath: "/path/to/tables.md",
fileName: "tables.md",
contents:
"| Header 1 | Header 2 |\n| -------- | -------- |\n| Cell 1 | Cell 2 |",
pageTitle: "Tables",
frontmatter: {
title: "Tables",
description: "A Markdown file demonstrating tables.",
},
},
{
folderName: "blockquotes",
absoluteFilePath: "/path/to/blockquotes.md",
fileName: "blockquotes.md",
contents: "> Blockquote example\n\n> Another blockquote example",
pageTitle: "Blockquotes",
frontmatter: {
title: "Blockquotes",
description: "A Markdown file demonstrating blockquotes.",
},
},
{
folderName: "horizontal_rules",
absoluteFilePath: "/path/to/horizontal_rules.md",
fileName: "horizontal_rules.md",
contents: "---\n\n***\n\n___",
pageTitle: "Horizontal Rules",
frontmatter: {
title: "Horizontal Rules",
description:
"A Markdown file demonstrating different horizontal rule styles.",
},
},
/*
{
folderName: "inline_html",
absoluteFilePath: "/path/to/inline_html.md",
fileName: "inline_html.md",
contents:
"<p>Paragraph with <strong>bold</strong> and <em>italic</em> text.</p>",
pageTitle: "Inline HTML",
frontmatter: {
title: "Inline HTML",
description:
"A Markdown file demonstrating the use of inline HTML.",
},
},
*/
{
folderName: "escaping",
absoluteFilePath: "/path/to/escaping.md",
fileName: "escaping.md",
contents: "\\*Escape asterisks\\*\n\n\\[Escape brackets\\]",
pageTitle: "Escaping",
frontmatter: {
title: "Escaping",
description:
"A Markdown file demonstrating how to escape special characters.",
},
},
{
folderName: "folder1",
absoluteFilePath: "/path/to/folder1/file1.md",
fileName: "file1.md",
contents: "# Test Content\nHello, World!",
pageTitle: "Test Page",
frontmatter: {
"connie-title": "Custom Title",
"connie-frontmatter-to-publish": ["author", "date"],
author: "John Doe",
date: "2023-04-14",
tags: ["test", "example"],
// "connie-page-id": "12345",
// "connie-dont-change-parent-page": true,
},
},
{
folderName: "folder2",
absoluteFilePath: "/path/to/folder2/file2.md",
fileName: "file2.md",
contents: "## Another Test\nThis is another test.",
pageTitle: "Another Test Page",
frontmatter: {
"connie-title": "Another Custom Title",
"connie-frontmatter-to-publish": ["project"],
project: "Project Name",
tags: ["demo", 42],
// "connie-page-id": 67890,
// "connie-dont-change-parent-page": false,
},
},
{
folderName: "folder3",
absoluteFilePath: "/path/to/folder3/file3.md",
fileName: "file3.md",
contents: "### Third Test\nYet another test content.",
pageTitle: "Third Test Page",
frontmatter: {
"connie-title": 98765,
"connie-frontmatter-to-publish": [],
tags: ["sample", "test"],
// "connie-page-id": "qwerty",
// "connie-dont-change-parent-page": "invalid",
},
},
];
class TestMermaidRenderer implements MermaidRenderer {
async captureMermaidCharts(
_charts: ChartData[]
): Promise<Map<string, Buffer>> {
const capturedCharts = new Map<string, Buffer>();
return capturedCharts;
}
}
class InMemoryAdaptor implements LoaderAdaptor {
private inMemoryFiles: MarkdownFile[];
constructor(inMemoryFiles: MarkdownFile[]) {
this.inMemoryFiles = inMemoryFiles;
}
async updateMarkdownValues(
_absoluteFilePath: string,
_values: Partial<ConfluencePerPageAllValues>
): Promise<void> {}
async loadMarkdownFile(absoluteFilePath: string): Promise<MarkdownFile> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.inMemoryFiles.find(
(t) => t.absoluteFilePath === absoluteFilePath
)!;
}
async getMarkdownFilesToUpload(): Promise<FilesToUpload> {
return this.inMemoryFiles;
}
async readBinary(
_path: string,
_referencedFromFilePath: string
): Promise<false | BinaryFile> {
throw new Error("Method not implemented.");
}
}
test("Upload to Confluence", async () => {
const filesystemAdaptor = new InMemoryAdaptor(markdownTestCases);
const mermaidRenderer = new TestMermaidRenderer();
const confluenceClient = new ConfluenceClient({
host: settings.confluenceBaseUrl,
authentication: {
basic: {
email: settings.atlassianUserName,
apiToken: settings.atlassianApiToken,
},
},
});
const searchParams = {
type: "page",
space: "it",
title: "Test - bf8bb13d-21b4-31b6-4584-8b9683d82086",
expand: ["version", "body.atlas_doc_format", "ancestors"],
};
const contentByTitle = await confluenceClient.content.getContent(
searchParams
);
const pageResult = contentByTitle.results[0];
if (!pageResult) {
throw new Error("Missing Parent Page");
}
settings.confluenceParentId = pageResult.id;
const settingLoaders = [
new EnvironmentVariableSettingsLoader(),
new StaticSettingsLoader({
confluenceParentId: pageResult.id,
}),
new DefaultSettingsLoader(),
];
const publisherSettingsLoader = new AutoSettingsLoader(settingLoaders);
const publisher = new Publisher(
filesystemAdaptor,
publisherSettingsLoader,
confluenceClient,
mermaidRenderer
);
const result = await publisher.publish();
for (const uploadResult of result) {
const afterUpload = await confluenceClient.content.getContentById({
id: uploadResult.node.file.pageId,
expand: ["body.atlas_doc_format", "space"],
});
const uploadedAdf = orderMarks(
JSON.parse(afterUpload.body?.atlas_doc_format?.value ?? "{}")
);
const returnedAdf = orderMarks(uploadResult.node.file.contents);
expect(returnedAdf).toEqual(uploadedAdf);
}
}, 300000);