2022-12-03 21:44:20 +00:00
|
|
|
import { readFile, writeFile, unlink } from 'node:fs/promises';
|
2022-12-15 20:49:51 +00:00
|
|
|
import { emojiVersion } from '../lib/emoji/data';
|
2022-12-03 21:44:20 +00:00
|
|
|
import { getEmojiSequenceFromString } from '../lib/emoji/cleanup';
|
|
|
|
import { getEmojiSequenceString } from '../lib/emoji/format';
|
2022-12-24 18:44:29 +00:00
|
|
|
import { parseEmojiTestFile } from '../lib/emoji/test/parse';
|
2022-12-16 08:48:17 +00:00
|
|
|
import { getQualifiedEmojiVariations } from '../lib/emoji/test/variations';
|
2022-12-03 21:44:20 +00:00
|
|
|
|
2022-12-15 09:58:57 +00:00
|
|
|
describe('Qualified variations of emoji sequences', () => {
|
2022-12-15 20:49:51 +00:00
|
|
|
async function fetchEmojiTestData(): Promise<string | undefined> {
|
|
|
|
// Fetch emojis, cache it
|
|
|
|
const source = `tests/fixtures/download-emoji-${emojiVersion}.txt`;
|
|
|
|
|
|
|
|
let data: string | undefined;
|
|
|
|
try {
|
|
|
|
data = await readFile(source, 'utf8');
|
|
|
|
} catch {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = (
|
|
|
|
await fetch(
|
|
|
|
`https://unicode.org/Public/emoji/${emojiVersion}/emoji-test.txt`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.text()
|
|
|
|
.toString();
|
|
|
|
await writeFile(source, data, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test content, unlink cache on failure
|
|
|
|
if (data.indexOf(`# Version: ${emojiVersion}`) === -1) {
|
|
|
|
try {
|
|
|
|
await unlink(source);
|
|
|
|
} catch {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
it('Variations without test data', () => {
|
2022-12-03 21:44:20 +00:00
|
|
|
const sequences = [
|
|
|
|
// simple emoji, twice to check duplicates
|
|
|
|
'1F601',
|
|
|
|
'1F601 FE0F',
|
|
|
|
// 2 simple emojis
|
|
|
|
'1F635 200D 1F4AB',
|
|
|
|
// simple emoji with variation
|
|
|
|
'00A9 FE0F',
|
|
|
|
// keycap with and without variation
|
|
|
|
'0031 FE0F 20E3',
|
|
|
|
'0033 20E3',
|
|
|
|
// complex emojis
|
|
|
|
'1F1E6 1F1F8',
|
|
|
|
'1F3F4 E0067 E0062 E0065 E006E E0067 E007F',
|
|
|
|
// mix of simple and complex, with and without variation
|
|
|
|
'1F9D7 1F3FE 200D 2640 FE0F',
|
|
|
|
'1F9D7 1F3FF 200D 2642 ',
|
2022-12-24 18:44:29 +00:00
|
|
|
].map((source) => {
|
|
|
|
const sequence = getEmojiSequenceFromString(source);
|
|
|
|
return {
|
|
|
|
source,
|
|
|
|
sequence,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-12-16 08:48:17 +00:00
|
|
|
const results = getQualifiedEmojiVariations(sequences);
|
2022-12-03 21:44:20 +00:00
|
|
|
expect(
|
2022-12-24 18:44:29 +00:00
|
|
|
results.map((item) =>
|
|
|
|
getEmojiSequenceString(item.sequence, {
|
2022-12-03 21:44:20 +00:00
|
|
|
separator: ' ',
|
|
|
|
case: 'upper',
|
|
|
|
format: 'utf-32',
|
|
|
|
add0: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).toEqual([
|
|
|
|
// simple emoji
|
|
|
|
'1F601 FE0F',
|
|
|
|
// 2 simple emojis
|
|
|
|
'1F635 FE0F 200D 1F4AB FE0F',
|
|
|
|
// simple emoji with variation
|
|
|
|
'00A9 FE0F',
|
|
|
|
// keycap with and without variation
|
|
|
|
'0031 FE0F 20E3',
|
|
|
|
'0033 FE0F 20E3',
|
|
|
|
// complex emojis
|
|
|
|
'1F1E6 1F1F8',
|
|
|
|
'1F3F4 E0067 E0062 E0065 E006E E0067 E007F',
|
|
|
|
// mix of simple and complex, with and without variation
|
|
|
|
'1F9D7 1F3FE 200D 2640 FE0F',
|
|
|
|
'1F9D7 1F3FF 200D 2642 FE0F',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-15 20:49:51 +00:00
|
|
|
it('Variations with test data', async () => {
|
2022-12-03 21:44:20 +00:00
|
|
|
// Fetch emojis, cache it
|
2022-12-15 20:49:51 +00:00
|
|
|
const data = await fetchEmojiTestData();
|
2022-12-03 21:44:20 +00:00
|
|
|
if (!data) {
|
2022-12-15 20:49:51 +00:00
|
|
|
console.warn('Test skipped: test data is not available');
|
2022-12-03 21:44:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const testData = parseEmojiTestFile(data);
|
|
|
|
|
2022-12-24 18:44:29 +00:00
|
|
|
// Make sure testData keys contain only unqualified emojis
|
|
|
|
const testDataStrings = new Set(Object.keys(testData));
|
2022-12-14 18:49:50 +00:00
|
|
|
expect(testDataStrings.has('1f600')).toBe(true);
|
|
|
|
expect(testDataStrings.has('263a')).toBe(true);
|
2022-12-24 18:44:29 +00:00
|
|
|
expect(testDataStrings.has('263a-fe0f')).toBe(false);
|
2022-12-14 18:49:50 +00:00
|
|
|
|
2022-12-24 18:44:29 +00:00
|
|
|
// Make sure values contain qualified emojis
|
|
|
|
expect(testData['1f600'].sequence).toEqual([0x1f600]);
|
|
|
|
expect(testData['263a'].sequence).toEqual([0x263a, 0xfe0f]);
|
2022-12-14 18:49:50 +00:00
|
|
|
|
|
|
|
// Sequences to test
|
2022-12-03 21:44:20 +00:00
|
|
|
const sequences = [
|
|
|
|
// emoji without variation in test file
|
|
|
|
'1F601',
|
|
|
|
'1F635 200D 1F4AB',
|
|
|
|
// emojis without variations in test file, but variations in source
|
|
|
|
'1F60D FE0F',
|
|
|
|
// emoji that has variation in test file
|
|
|
|
'263A',
|
|
|
|
// keycap
|
|
|
|
'0030 20E3',
|
|
|
|
'0034 FE0F 20E3',
|
|
|
|
// complex emoji, exists in file
|
|
|
|
'1F9D1 1F3FE 200D 2764 200D 1F9D1 1F3FB',
|
|
|
|
// simple emoji, not in test file
|
2022-12-24 18:44:29 +00:00
|
|
|
'25F0',
|
2022-12-03 21:44:20 +00:00
|
|
|
// fake keycap, not in test file
|
|
|
|
'2345 20E3 200D 1235',
|
2022-12-24 18:44:29 +00:00
|
|
|
].map((source) => {
|
|
|
|
const sequence = getEmojiSequenceFromString(source);
|
|
|
|
return {
|
|
|
|
source,
|
|
|
|
sequence,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const results = getQualifiedEmojiVariations(sequences, testData);
|
2022-12-03 21:44:20 +00:00
|
|
|
expect(
|
2022-12-24 18:44:29 +00:00
|
|
|
results.map((item) =>
|
|
|
|
getEmojiSequenceString(item.sequence, {
|
2022-12-03 21:44:20 +00:00
|
|
|
separator: ' ',
|
|
|
|
case: 'upper',
|
|
|
|
format: 'utf-32',
|
|
|
|
add0: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).toEqual([
|
|
|
|
// emoji without variation in test file
|
|
|
|
'1F601',
|
|
|
|
'1F635 200D 1F4AB',
|
|
|
|
// emojis without variations in test file, but variations in source
|
|
|
|
'1F60D',
|
|
|
|
// emoji that has variation in test file
|
|
|
|
'263A FE0F',
|
|
|
|
// keycap
|
|
|
|
'0030 FE0F 20E3',
|
|
|
|
'0034 FE0F 20E3',
|
|
|
|
// complex emoji, exists in file
|
|
|
|
'1F9D1 1F3FE 200D 2764 FE0F 200D 1F9D1 1F3FB',
|
|
|
|
// simple emoji, not in test file
|
2022-12-24 18:44:29 +00:00
|
|
|
'25F0 FE0F',
|
2022-12-03 21:44:20 +00:00
|
|
|
// fake keycap, not in test file
|
|
|
|
'2345 FE0F 20E3 200D 1235 FE0F',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|