-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtest.js
More file actions
122 lines (100 loc) · 3.87 KB
/
test.js
File metadata and controls
122 lines (100 loc) · 3.87 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
import test from 'ava';
import 'babel-register';
import 'babel-polyfill';
import fs from 'fs';
import { execFile } from 'child_process';
import sqlite3 from 'sqlite3';
import isArrayBufferEqual from 'arraybuffer-equal';
import pify from 'pify';
import sinon from 'sinon';
import sortBy from 'lodash.sortby';
import AnkiExport from '../src';
import { addCards, unzipDeckToDir } from './_helpers';
const tmpDir = '/tmp';
const dest = tmpDir + '/result.apkg';
const destUnpacked = tmpDir + '/unpacked_result';
const destUnpackedDb = destUnpacked + '/collection.anki2';
const SEPARATOR = '\u001F';
test.beforeEach(async () => pify(execFile)('rm', ['-rf', dest, destUnpacked]));
test('equals to sample', async t => {
const now = 1482680798652;
const sandbox = sinon.sandbox.create();
const clock = sinon.useFakeTimers(now);
const apkg = new AnkiExport('deck-name');
apkg.addMedia('anki.png', fs.readFileSync(__dirname + '/fixtures/anki.png'));
apkg.addCard(['card #1 front', 'card #1 back'], { tags: ['food', 'fruit'] });
apkg.addCard(['card #2 front', 'card #2 back']);
apkg.addCard(['card #3 with image <img src="anki.png" />', 'card #3 back']);
const zip = await apkg.save();
fs.writeFileSync(dest, zip, 'binary');
t.true(zip instanceof Buffer);
const sampleZip = fs.readFileSync(`${__dirname}/fixtures/output.apkg`);
const destZip = fs.readFileSync(dest);
t.true(isArrayBufferEqual(destZip.buffer, sampleZip.buffer));
sandbox.restore();
clock.restore();
});
test('check internal structure', async t => {
// Create deck as in previous example
const apkg = new AnkiExport('deck-name');
const cards = [
{ front: 'card #1 front', back: 'card #1 back' },
{ front: 'card #2 front', back: 'card #2 back' },
{ front: 'card #3 with image <img src="anki.png" />', back: 'card #3 back' }
];
addCards(apkg, cards);
const zip = await apkg.save();
fs.writeFileSync(dest, zip, 'binary');
// extract dec to tmp directory
await unzipDeckToDir(dest, destUnpacked);
// analize db via sqlite
const db = new sqlite3.Database(destUnpackedDb);
const result = await pify(db.all.bind(db))(
`SELECT
notes.sfld as front,
notes.flds as back
from cards JOIN notes where cards.nid = notes.id ORDER BY cards.id`
);
db.close();
// compare content from just created db with original list of cards
const normilizedResult = sortBy(
result.map(({ front, back }) => ({
front,
back: back.split(SEPARATOR).pop()
})),
'front'
);
t.deepEqual(normilizedResult, cards);
});
test('check internal structure on adding card with tags', async t => {
const decFile = `${dest}_with_tags.apkg`;
const unzipedDeck = `${destUnpacked}_with_tags`;
const apkg = new AnkiExport('deck-name');
const [front1, back1, tags1] = ['Card front side 1', 'Card back side 1', ['some', 'tag', 'tags with multiple words']];
const [front2, back2, tags2] = ['Card front side 2', 'Card back side 2', 'some strin_tags'];
const [front3, back3] = ['Card front side 3', 'Card back side 3'];
apkg.addCard([front1, back1], { tags: tags1 });
apkg.addCard([front2, back2], { tags: tags2 });
apkg.addCard([front3, back3]);
const zip = await apkg.save();
fs.writeFileSync(decFile, zip, 'binary');
await unzipDeckToDir(decFile, unzipedDeck);
const db = new sqlite3.Database(`${unzipedDeck}/collection.anki2`);
const results = await pify(db.all.bind(db))(
`SELECT
notes.sfld as front,
notes.flds as back,
notes.tags as tags
from cards JOIN notes where cards.nid = notes.id ORDER BY front`
);
db.close();
t.deepEqual(results, [
{
front: front1,
back: `${front1}${SEPARATOR}${back1}`,
tags: ' ' + tags1.map(tag => tag.replace(/ /g, '_')).join(' ') + ' '
},
{ front: front2, back: `${front2}${SEPARATOR}${back2}`, tags: tags2 },
{ front: front3, back: `${front3}${SEPARATOR}${back3}`, tags: '' }
]);
});