-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathpaste.test.ts
More file actions
96 lines (84 loc) · 2.95 KB
/
Copy pathpaste.test.ts
File metadata and controls
96 lines (84 loc) · 2.95 KB
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
import { describe, expect, it } from 'vitest'
import {
assessTextPaste,
countPasteRows,
formatPasteLimit,
PASTE_LIMITS,
utf8ByteLength,
utf8ByteLengthRange,
} from './paste'
describe('utf8ByteLength', () => {
it('matches UTF-8 for ASCII, BMP, astral, and malformed UTF-16', () => {
for (const value of ['plain text', 'café', '你好', '💡', '\ud800']) {
expect(utf8ByteLength(value)).toBe(new TextEncoder().encode(value).byteLength)
}
})
it('short-circuits once the limit is exceeded', () => {
expect(utf8ByteLength('a'.repeat(1_000_000), 100)).toBe(101)
})
it('measures a range without slicing the source', () => {
expect(utf8ByteLengthRange('a💡b', 1, 3)).toBe(4)
})
})
describe('assessTextPaste', () => {
it('accepts a selection replacement at the projected boundary', () => {
expect(
assessTextPaste({
pastedText: '💡',
currentText: '123456',
selectionStart: 1,
selectionEnd: 5,
maxResultBytes: 6,
})
).toMatchObject({ accepted: true, resultBytes: 6 })
})
it('rejects payload characters before scanning bytes', () => {
expect(
assessTextPaste({ pastedText: 'abc', maxPastedCharacters: 2, maxPastedBytes: 100 })
).toEqual({ accepted: false, reason: 'pasted-characters', actual: 3, limit: 2 })
})
it('rejects non-ASCII payloads by exact byte size', () => {
expect(assessTextPaste({ pastedText: '💡', maxPastedBytes: 3 })).toEqual({
accepted: false,
reason: 'pasted-bytes',
actual: 4,
limit: 3,
})
})
it('accepts payloads with worst-case UTF-8 headroom without measuring exact bytes', () => {
expect(assessTextPaste({ pastedText: '💡'.repeat(100), maxPastedBytes: 1_000 })).toEqual({
accepted: true,
})
})
it('rejects a projected result above its character limit', () => {
expect(
assessTextPaste({
pastedText: 'xyz',
currentText: 'abcd',
selectionStart: 1,
selectionEnd: 2,
maxResultCharacters: 5,
})
).toEqual({ accepted: false, reason: 'result-characters', actual: 6, limit: 5 })
})
})
describe('countPasteRows', () => {
it('handles LF, CRLF, and CR without allocating rows', () => {
expect(countPasteRows('a\nb\r\nc\rd')).toBe(4)
expect(countPasteRows('')).toBe(0)
})
it('short-circuits above a row ceiling', () => {
expect(countPasteRows('a\nb\nc\nd', 2)).toBe(3)
})
})
it('formats binary paste limits', () => {
expect(formatPasteLimit(256 * 1024)).toBe('256 KiB')
expect(formatPasteLimit(1_000_000)).toBe('1 MB')
expect(formatPasteLimit(5 * 1024 * 1024)).toBe('5 MiB')
})
it('keeps crash-only ceilings high and aligns file editing with its content contract', () => {
expect(PASTE_LIMITS.DEFAULT_BYTES).toBe(32 * 1024 * 1024)
expect(PASTE_LIMITS.TEXT_EDITOR_BYTES).toBe(50 * 1024 * 1024)
expect(PASTE_LIMITS.TERMINAL_BYTES).toBe(8 * 1024 * 1024)
expect(PASTE_LIMITS.STRUCTURED_BYTES).toBe(32 * 1024 * 1024)
})