forked from logseq/logseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.spec.ts
139 lines (121 loc) · 5.07 KB
/
fs.spec.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
import fsp from 'fs/promises';
import path from 'path';
import { expect } from '@playwright/test'
import { test } from './fixtures';
import { searchPage, captureConsoleWithPrefix, closeSearchBox, createPage, IsWindows } from './utils';
test('create file on disk then delete', async ({ page, block, graphDir }) => {
// Since have to wait for file watchers
test.slow();
// Special page names: namespaced, chars require escaping, chars require unicode normalization, "%" chars, "%" with 2 hexdigests
const testCases = [
{pageTitle: "User:John", fileName: "User%3AJohn"},
// invalid url decode escaping as %ff is not parsable but match the common URL encode regex
{pageTitle: "#%ff", fileName: "#%ff"},
// valid url decode escaping
{pageTitle: "#%23", fileName: "#%2523"},
{pageTitle: "@!#%", fileName: "@!#%"},
{pageTitle: "aàáâ", fileName: "aàáâ"},
{pageTitle: "#%gggg", fileName: "#%gggg"}
]
if (!IsWindows)
testCases.push({pageTitle: "User:Bob", fileName: "User:Bob"})
function getFullPath(fileName: string) {
return path.join(graphDir, "pages", `${fileName}.md`);
}
// Test putting files on disk
for (const {pageTitle, fileName} of testCases) {
// Put the file on disk
const filePath = getFullPath(fileName);
await fsp.writeFile(filePath, `- content for ${pageTitle}`);
await captureConsoleWithPrefix(page, "Parsing finished:", 5000)
// Check that the page is created
const results = await searchPage(page, pageTitle);
const firstResultRow = await results[0].innerText()
expect(firstResultRow).toContain(pageTitle);
expect(firstResultRow).not.toContain("Create");
await closeSearchBox(page);
}
// Test removing files on disk
for (const {pageTitle, fileName} of testCases) {
// Remove the file on disk
const filePath = getFullPath(fileName);
await fsp.unlink(filePath);
await captureConsoleWithPrefix(page, "Delete page:", 5000);
// Test that the page is deleted
const results = await searchPage(page, pageTitle);
const firstResultRow = await results[0].innerText()
// expect(firstResultRow).toContain("Create");
await closeSearchBox(page);
}
});
test("Rename file on disk", async ({ page, block, graphDir }) => {
// Since have to wait for file watchers
test.slow();
const testCases = [
// Normal -> NameSpace
{pageTitle: "User:John", fileName: "User%3AJohn",
newPageTitle: "User/John", newFileName: "User___John"},
// NameSpace -> Normal
{pageTitle: "#/%23", fileName: "#___%2523",
newPageTitle: "#%23", newFileName: "#%2523"}
]
if (!IsWindows)
testCases.push({pageTitle: "User:Bob", fileName: "User:Bob",
newPageTitle: "User/Bob", newFileName: "User___Bob"})
function getFullPath(fileName: string) {
return path.join(graphDir, "pages", `${fileName}.md`);
}
// Test putting files on disk
for (const {pageTitle, fileName} of testCases) {
// Put the file on disk
const filePath = getFullPath(fileName);
await fsp.writeFile(filePath, `- content for ${pageTitle}`);
await captureConsoleWithPrefix(page, "Parsing finished:", 5000)
// Check that the page is created
const results = await searchPage(page, pageTitle);
const firstResultRow = await results[0].innerText()
expect(firstResultRow).toContain(pageTitle);
expect(firstResultRow).not.toContain("Create");
await closeSearchBox(page);
}
// Test renaming files on disk
for (const {pageTitle, fileName, newPageTitle, newFileName} of testCases) {
// Rename the file on disk
const filePath = getFullPath(fileName);
const newFilePath = getFullPath(newFileName);
await fsp.rename(filePath, newFilePath);
await captureConsoleWithPrefix(page, "Parsing finished:", 5000);
await page.waitForTimeout(500);
// Test that the page is renamed
const results = await searchPage(page, newPageTitle);
const firstResultRow = await results[0].innerText()
expect(firstResultRow).toContain(newPageTitle);
expect(firstResultRow).not.toContain(pageTitle);
expect(firstResultRow).not.toContain("Create");
await closeSearchBox(page);
}
})
test('special page names', async ({ page, block, graphDir }) => {
const testCases = [
{pageTitle: "User:John", fileName: "User%3AJohn"},
{pageTitle: "_#%ff", fileName: "_%23%25ff"},
{pageTitle: "@!#%", fileName: "@!%23%"},
{pageTitle: "aàáâ", fileName: "aàáâ"},
{pageTitle: "_#%gggg", fileName: "_%23%gggg"}
]
// Test putting files on disk
for (const {pageTitle, fileName} of testCases) {
// Create page in Logseq
await createPage(page, pageTitle)
const text = `content for ${pageTitle}`
await block.mustFill(text)
await page.keyboard.press("Enter", { delay: 50 })
await page.keyboard.press("Escape", { delay: 50 })
// Wait for the file to be created on disk
await page.waitForTimeout(2500);
// Validate that the file is created on disk with the content
const filePath = path.join(graphDir, "pages", `${fileName}.md`);
const fileContent = await fsp.readFile(filePath, "utf8");
expect(fileContent).toContain(text);
}
});