Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
241 lines (204 loc) · 7.22 KB
/
Copy pathtest.js
File metadata and controls
241 lines (204 loc) · 7.22 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
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
import {EOL, tmpdir} from 'node:os';
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import test from 'ava';
import browserClipboard from './browser.js';
import wayland from './lib/wayland.js';
import clipboard from './index.js';
const writeRead = async input => {
await clipboard.write(input);
return clipboard.read();
};
const writeReadSync = input => {
clipboard.writeSync(input);
return clipboard.readSync();
};
// Basic functionality tests
test('basic async operation', async t => {
t.is(await writeRead('test'), 'test');
});
test('basic sync operation', t => {
t.is(writeReadSync('test'), 'test');
});
// Content type tests
test('handles various content types', async t => {
const testCases = [
'', // Empty string
'simple text',
'123456789abcdefghijklmnopqrstuvwxyz+-=&_[]<^=>=/{:})-{(`)}', // ASCII special chars
'ĀāĂ㥹ĆćĈĉĊċČčĎ ፰፱፲፳፴፵፶፷፸፹፺፻፼ æøå ± 你好', // Unicode mix
'🦄❤️🤘🐑💩', // Emojis
' leading and trailing spaces ',
`line${EOL}break`,
`ending newline${EOL}`,
'foo\tbar\nbaz\r\nqux', // Various whitespace
'x'.repeat(1000), // Longer string
];
// Test each case sequentially to avoid clipboard conflicts
for (const fixture of testCases) {
t.is(await writeRead(fixture), fixture); // eslint-disable-line no-await-in-loop
}
});
test('handles various content types (sync)', t => {
const testCases = [
'',
'sync test',
'🎉 sync unicode 测试',
' sync spaces ',
`sync${EOL}newline`,
];
for (const fixture of testCases) {
t.is(writeReadSync(fixture), fixture);
}
});
if (process.platform === 'win32') {
test('preserves newlines from stdin on Windows', async t => {
const input = 'line one\nline two\n';
t.is(await writeRead(input), input);
});
}
const countTemporaryFiles = () => fs.readdirSync(tmpdir()).filter(name => name.startsWith('clipboardy-')).length;
if (process.platform !== 'win32') {
const createFakeWlCopyEnvironment = () => {
const temporaryDirectory = fs.mkdtempSync(path.join(import.meta.dirname, '.ai-temporary-'));
const fakeWlCopyPath = path.join(temporaryDirectory, 'wl-copy');
const backgroundProcessIdPath = path.join(temporaryDirectory, 'background-process-id');
fs.writeFileSync(fakeWlCopyPath, '#!/bin/sh\nsleep 5 &\necho $! > "$FAKE_WL_COPY_PID_FILE"\nexit 0\n', {mode: 0o755});
return {
temporaryDirectory,
cleanup() {
try {
const backgroundProcessId = Number(fs.readFileSync(backgroundProcessIdPath, 'utf8'));
if (backgroundProcessId > 0) {
process.kill(backgroundProcessId);
}
} catch {}
fs.rmSync(temporaryDirectory, {recursive: true});
},
environment: {
...process.env,
PATH: `${temporaryDirectory}${path.delimiter}${process.env.PATH}`,
FAKE_WL_COPY_PID_FILE: backgroundProcessIdPath,
},
};
};
test('Wayland write() does not wait for the clipboard to be taken over', async t => {
const {cleanup, environment} = createFakeWlCopyEnvironment();
try {
const before = countTemporaryFiles();
const start = performance.now();
await wayland.copy({input: 'no hang', env: environment});
t.true(performance.now() - start < 2000);
t.is(countTemporaryFiles(), before);
} finally {
cleanup();
}
});
test('Wayland writeSync() does not wait for the clipboard to be taken over', t => {
const {cleanup, environment} = createFakeWlCopyEnvironment();
try {
const before = countTemporaryFiles();
const start = performance.now();
wayland.copySync({input: 'no hang', env: environment});
t.true(performance.now() - start < 2000);
t.is(countTemporaryFiles(), before);
} finally {
cleanup();
}
});
}
if (process.platform === 'linux') {
// On Wayland, `wl-copy` keeps serving the clipboard in a background process, which must not keep us waiting nor leave its stderr file behind (https://github.com/sindresorhus/clipboardy/issues/111)
test('write() does not wait for the clipboard to be taken over', async t => {
const start = performance.now();
await clipboard.write('no hang');
t.true(performance.now() - start < 2000);
});
test('writeSync() does not wait for the clipboard to be taken over', t => {
const start = performance.now();
clipboard.writeSync('no hang');
t.true(performance.now() - start < 2000);
});
test('write() does not leave temporary files behind', async t => {
const before = countTemporaryFiles();
await clipboard.write('no leftovers');
t.is(countTemporaryFiles(), before);
});
test('writeSync() does not leave temporary files behind', t => {
const before = countTemporaryFiles();
clipboard.writeSync('no leftovers');
t.is(countTemporaryFiles(), before);
});
}
// Large input test (reproduces https://github.com/sindresorhus/clipboard-cli/issues/9)
test('handles large multiline input', async t => {
const line = 'I am a pink elephant. Do not think about me';
const lineCount = 5000;
const input = Array.from({length: lineCount}, () => line).join('\n');
const output = await writeRead(input);
t.is(output, input);
});
// Error handling tests
test('write() rejects non-string input', async t => {
const invalidInputs = [123, null, undefined, {}, [], true];
for (const input of invalidInputs) {
await t.throwsAsync( // eslint-disable-line no-await-in-loop
async () => clipboard.write(input),
{instanceOf: TypeError, message: /Expected a string/},
);
}
});
test('writeSync() throws on non-string input', t => {
const invalidInputs = [123, null, undefined, {}, [], true];
for (const input of invalidInputs) {
t.throws(
() => clipboard.writeSync(input),
{instanceOf: TypeError, message: /Expected a string/},
);
}
});
test('browser export provides image stubs', async t => {
t.is(typeof browserClipboard.writeImages, 'function');
t.is(typeof browserClipboard.readImages, 'function');
t.is(typeof browserClipboard.hasImages, 'function');
await browserClipboard.writeImages([]);
t.deepEqual(await browserClipboard.readImages(), []);
t.false(await browserClipboard.hasImages());
});
// Image clipboard tests
test('writeImages() rejects non-array input', async t => {
await t.throwsAsync(
async () => clipboard.writeImages('not-an-array'),
{instanceOf: TypeError, message: /Expected an array/},
);
});
if (process.platform === 'darwin') {
test('image round-trip', async t => {
// Create a minimal valid PNG
const temporaryDirectory = fs.mkdtempSync(path.join(import.meta.dirname, 'tmp-'));
const imagePath = path.join(temporaryDirectory, 'test.png');
// 8x8 RGB PNG
const pngBuffer = Buffer.from(
'89504e470d0a1a0a0000000d49484452000000080000000808020000004b6d29dc0000001849444154789c6260a0040098e7f84f0000f3ff03f40810061c0089f9057f2a4c29970000000049454e44ae426082',
'hex',
);
fs.writeFileSync(imagePath, pngBuffer);
try {
await clipboard.writeImages([imagePath]);
t.true(await clipboard.hasImages());
const readPaths = await clipboard.readImages();
t.true(readPaths.length > 0);
for (const filePath of readPaths) {
t.true(fs.existsSync(filePath));
fs.unlinkSync(filePath);
}
} finally {
fs.rmSync(temporaryDirectory, {recursive: true});
}
});
test('hasImages() returns false after writing text', async t => {
await clipboard.write('text content');
t.false(await clipboard.hasImages());
});
}