mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 02:09:23 +08:00
Continues #2978 and closes #2887. Includes the original Windows file actions, integrates the changes with dev, and adds an explicit default-app action. Co-authored-by: Botong Wu <2048400180@qq.com>
48 lines
2.5 KiB
JavaScript
48 lines
2.5 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { canCopyFileText, mayOpenInBrowser, readFileText } from '../src/file-content.mjs';
|
|
|
|
test('clipboard text uses the served file bytes and keeps line endings', async () => {
|
|
const source = '# 会话资料\r\n\r\n内容\r\n';
|
|
const response = new Response(source, { headers: { 'content-type': 'text/markdown' } });
|
|
assert.equal(await readFileText(response, '资料.md'), source);
|
|
|
|
const utf16 = Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(source, 'utf16le')]);
|
|
assert.equal(await readFileText(new Response(utf16), '资料.txt'), source);
|
|
});
|
|
|
|
test('file contents is unavailable for binary or oversized files', async () => {
|
|
assert.equal(canCopyFileText(new Headers({ 'content-type': 'application/pdf' }), 'file.pdf'), false);
|
|
assert.equal(canCopyFileText(new Headers({ 'content-type': 'application/vnd.ms-excel' }), 'report.csv'), true);
|
|
const large = new Response(new Uint8Array(8 * 1024 * 1024 + 1), {
|
|
headers: { 'content-type': 'text/plain' },
|
|
});
|
|
await assert.rejects(readFileText(large, 'notes.txt'), /exceeds the text limit/);
|
|
const invalid = new Response(new Uint8Array([0xc3, 0x28]), {
|
|
headers: { 'content-type': 'text/plain; charset=utf-8' },
|
|
});
|
|
await assert.rejects(readFileText(invalid, 'notes.txt'), TypeError);
|
|
});
|
|
|
|
test('Windows TypeScript MIME alias enables text copy without assuming browser video support', async () => {
|
|
const headers = new Headers({ 'Content-Type': 'video/vnd.dlna.mpeg-tts' });
|
|
const source = 'const value: string = "text";\r\n';
|
|
assert.equal(canCopyFileText(headers, 'code.TS'), true);
|
|
assert.equal(await readFileText(new Response(source, { headers }), 'code.TS'), source);
|
|
assert.equal(mayOpenInBrowser(headers, 'code.TS'), false);
|
|
assert.equal(canCopyFileText(headers, 'video.m2ts'), false);
|
|
assert.equal(mayOpenInBrowser(headers, 'video.m2ts'), true);
|
|
assert.equal(mayOpenInBrowser(new Headers({ 'Content-Type': 'video/mp2t' }), 'video.ts'), true);
|
|
});
|
|
|
|
test('active document browser previews require an origin-isolating CSP sandbox', () => {
|
|
for (const mime of ['text/html', 'image/svg+xml']) {
|
|
const headers = new Headers({ 'Content-Type': mime });
|
|
assert.equal(mayOpenInBrowser(headers, 'document'), false);
|
|
headers.set('Content-Security-Policy', 'default-src none; sandbox allow-scripts');
|
|
assert.equal(mayOpenInBrowser(headers, 'document'), true);
|
|
headers.set('Content-Security-Policy', 'sandbox allow-same-origin');
|
|
assert.equal(mayOpenInBrowser(headers, 'document'), false);
|
|
}
|
|
});
|