添加自动运行广告条的样式和逻辑,并实现相关功能的测试
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
|
||||
const sidepanelSource = fs.readFileSync('sidepanel/sidepanel.js', 'utf8');
|
||||
|
||||
function extractFunction(name) {
|
||||
const markers = [`async function ${name}(`, `function ${name}(`];
|
||||
const start = markers
|
||||
.map((marker) => sidepanelSource.indexOf(marker))
|
||||
.find((index) => index >= 0);
|
||||
if (start < 0) {
|
||||
throw new Error(`missing function ${name}`);
|
||||
}
|
||||
|
||||
let parenDepth = 0;
|
||||
let signatureEnded = false;
|
||||
let braceStart = -1;
|
||||
for (let i = start; i < sidepanelSource.length; i += 1) {
|
||||
const ch = sidepanelSource[i];
|
||||
if (ch === '(') {
|
||||
parenDepth += 1;
|
||||
} else if (ch === ')') {
|
||||
parenDepth -= 1;
|
||||
if (parenDepth === 0) {
|
||||
signatureEnded = true;
|
||||
}
|
||||
} else if (ch === '{' && signatureEnded) {
|
||||
braceStart = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let depth = 0;
|
||||
let end = braceStart;
|
||||
for (; end < sidepanelSource.length; end += 1) {
|
||||
const ch = sidepanelSource[end];
|
||||
if (ch === '{') depth += 1;
|
||||
if (ch === '}') {
|
||||
depth -= 1;
|
||||
if (depth === 0) {
|
||||
end += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sidepanelSource.slice(start, end);
|
||||
}
|
||||
|
||||
const bundle = [
|
||||
extractFunction('sanitizeAutoRunAdUrl'),
|
||||
extractFunction('parseAutoRunAdSegments'),
|
||||
extractFunction('getAutoRunAdPlainText'),
|
||||
].join('\n');
|
||||
|
||||
const api = new Function(`
|
||||
${bundle}
|
||||
return {
|
||||
sanitizeAutoRunAdUrl,
|
||||
parseAutoRunAdSegments,
|
||||
getAutoRunAdPlainText,
|
||||
};
|
||||
`)();
|
||||
|
||||
test('sanitizeAutoRunAdUrl only allows http and https links', () => {
|
||||
assert.equal(api.sanitizeAutoRunAdUrl('https://example.com/docs'), 'https://example.com/docs');
|
||||
assert.equal(api.sanitizeAutoRunAdUrl('http://example.com/docs'), 'http://example.com/docs');
|
||||
assert.equal(api.sanitizeAutoRunAdUrl('javascript:alert(1)'), '');
|
||||
assert.equal(api.sanitizeAutoRunAdUrl('data:text/html,hello'), '');
|
||||
});
|
||||
|
||||
test('parseAutoRunAdSegments converts markdown links into safe link segments', () => {
|
||||
const segments = api.parseAutoRunAdSegments('查看 [教程](https://example.com/tutorial) 和 [频道](https://example.com/channel)');
|
||||
|
||||
assert.deepEqual(segments, [
|
||||
{ type: 'text', text: '查看 ' },
|
||||
{ type: 'link', text: '教程', url: 'https://example.com/tutorial' },
|
||||
{ type: 'text', text: ' 和 ' },
|
||||
{ type: 'link', text: '频道', url: 'https://example.com/channel' },
|
||||
]);
|
||||
assert.equal(api.getAutoRunAdPlainText(segments), '查看 教程 和 频道');
|
||||
});
|
||||
|
||||
test('parseAutoRunAdSegments keeps unsafe markdown links as plain text', () => {
|
||||
const segments = api.parseAutoRunAdSegments('危险链接 [不要点](javascript:alert(1))');
|
||||
|
||||
assert.deepEqual(segments, [
|
||||
{ type: 'text', text: '危险链接 ' },
|
||||
{ type: 'text', text: '[不要点](javascript:alert(1))' },
|
||||
]);
|
||||
});
|
||||
@@ -151,7 +151,7 @@ return {
|
||||
`)();
|
||||
}
|
||||
|
||||
test('startAutoRunFromCurrentSettings refreshes contribution content hint before starting auto run', async () => {
|
||||
test('startAutoRunFromCurrentSettings kicks off contribution content refresh without blocking auto run', async () => {
|
||||
const api = createApi();
|
||||
|
||||
const result = await api.startAutoRunFromCurrentSettings();
|
||||
@@ -175,10 +175,24 @@ test('startAutoRunFromCurrentSettings continues auto run when contribution conte
|
||||
assert.equal(result, true);
|
||||
assert.deepEqual(
|
||||
events.map((entry) => entry.type),
|
||||
['refresh', 'warn', 'sync-settings', 'send']
|
||||
['refresh', 'sync-settings', 'send', 'warn']
|
||||
);
|
||||
assert.equal(events[2].message.type, 'AUTO_RUN');
|
||||
assert.match(String(events[3].args[0]), /Failed to refresh contribution content hint before auto run/);
|
||||
});
|
||||
|
||||
test('startAutoRunFromCurrentSettings does not wait for a hanging contribution content refresh', async () => {
|
||||
const api = createApi({
|
||||
refreshImpl: '() => new Promise(() => {})',
|
||||
});
|
||||
|
||||
const result = await api.startAutoRunFromCurrentSettings();
|
||||
|
||||
assert.equal(result, true);
|
||||
assert.deepEqual(
|
||||
api.getEvents().map((entry) => entry.type),
|
||||
['refresh', 'sync-settings', 'send']
|
||||
);
|
||||
assert.match(String(events[1].args[0]), /Failed to refresh contribution content hint before auto run/);
|
||||
assert.equal(events[3].message.type, 'AUTO_RUN');
|
||||
});
|
||||
|
||||
test('startAutoRunFromCurrentSettings does not block auto run when contribution content has updates', async () => {
|
||||
|
||||
Reference in New Issue
Block a user