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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
|
import { randomBytes } from "node:crypto";
import { mkdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { DatabaseSync } from "node:sqlite";
import { deflateRawSync } from "node:zlib";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
type AnkiPackage,
listAnkiPackageContents,
mapAnkiToKioku,
parseAnkiPackage,
} from "./parser.js";
/**
* Create a minimal ZIP file with the given entries
*/
function createZip(entries: Map<string, Buffer>): Buffer {
const chunks: Buffer[] = [];
const centralDirectory: Buffer[] = [];
let offset = 0;
for (const [name, data] of entries) {
const nameBuffer = Buffer.from(name, "utf8");
const compressedData = deflateRawSync(data);
// Local file header
const localHeader = Buffer.alloc(30 + nameBuffer.length);
localHeader.writeUInt32LE(0x04034b50, 0); // signature
localHeader.writeUInt16LE(20, 4); // version needed
localHeader.writeUInt16LE(0, 6); // flags
localHeader.writeUInt16LE(8, 8); // compression method (deflate)
localHeader.writeUInt16LE(0, 10); // mod time
localHeader.writeUInt16LE(0, 12); // mod date
localHeader.writeUInt32LE(0, 14); // crc32 (not validated in our parser)
localHeader.writeUInt32LE(compressedData.length, 18); // compressed size
localHeader.writeUInt32LE(data.length, 22); // uncompressed size
localHeader.writeUInt16LE(nameBuffer.length, 26); // file name length
localHeader.writeUInt16LE(0, 28); // extra field length
nameBuffer.copy(localHeader, 30);
// Central directory entry
const centralEntry = Buffer.alloc(46 + nameBuffer.length);
centralEntry.writeUInt32LE(0x02014b50, 0); // signature
centralEntry.writeUInt16LE(20, 4); // version made by
centralEntry.writeUInt16LE(20, 6); // version needed
centralEntry.writeUInt16LE(0, 8); // flags
centralEntry.writeUInt16LE(8, 10); // compression method
centralEntry.writeUInt16LE(0, 12); // mod time
centralEntry.writeUInt16LE(0, 14); // mod date
centralEntry.writeUInt32LE(0, 16); // crc32
centralEntry.writeUInt32LE(compressedData.length, 20); // compressed size
centralEntry.writeUInt32LE(data.length, 24); // uncompressed size
centralEntry.writeUInt16LE(nameBuffer.length, 28); // file name length
centralEntry.writeUInt16LE(0, 30); // extra field length
centralEntry.writeUInt16LE(0, 32); // comment length
centralEntry.writeUInt16LE(0, 34); // disk number
centralEntry.writeUInt16LE(0, 36); // internal attributes
centralEntry.writeUInt32LE(0, 38); // external attributes
centralEntry.writeUInt32LE(offset, 42); // offset of local header
nameBuffer.copy(centralEntry, 46);
centralDirectory.push(centralEntry);
chunks.push(localHeader, compressedData);
offset += localHeader.length + compressedData.length;
}
// Central directory
const centralDirOffset = offset;
const centralDirBuffer = Buffer.concat(centralDirectory);
chunks.push(centralDirBuffer);
// End of central directory
const endRecord = Buffer.alloc(22);
endRecord.writeUInt32LE(0x06054b50, 0); // signature
endRecord.writeUInt16LE(0, 4); // disk number
endRecord.writeUInt16LE(0, 6); // disk with central dir
endRecord.writeUInt16LE(entries.size, 8); // entries on this disk
endRecord.writeUInt16LE(entries.size, 10); // total entries
endRecord.writeUInt32LE(centralDirBuffer.length, 12); // central dir size
endRecord.writeUInt32LE(centralDirOffset, 16); // central dir offset
endRecord.writeUInt16LE(0, 20); // comment length
chunks.push(endRecord);
return Buffer.concat(chunks);
}
/**
* Create a test Anki SQLite database
*/
function createTestAnkiDb(dbPath: string): void {
const db = new DatabaseSync(dbPath);
// Create tables
db.exec(`
CREATE TABLE col (
id INTEGER PRIMARY KEY,
crt INTEGER NOT NULL,
mod INTEGER NOT NULL,
scm INTEGER NOT NULL,
ver INTEGER NOT NULL,
dty INTEGER NOT NULL,
usn INTEGER NOT NULL,
ls INTEGER NOT NULL,
conf TEXT NOT NULL,
models TEXT NOT NULL,
decks TEXT NOT NULL,
dconf TEXT NOT NULL,
tags TEXT NOT NULL
)
`);
db.exec(`
CREATE TABLE notes (
id INTEGER PRIMARY KEY,
guid TEXT NOT NULL,
mid INTEGER NOT NULL,
mod INTEGER NOT NULL,
usn INTEGER NOT NULL,
tags TEXT NOT NULL,
flds TEXT NOT NULL,
sfld TEXT NOT NULL,
csum INTEGER NOT NULL,
flags INTEGER NOT NULL,
data TEXT NOT NULL
)
`);
db.exec(`
CREATE TABLE cards (
id INTEGER PRIMARY KEY,
nid INTEGER NOT NULL,
did INTEGER NOT NULL,
ord INTEGER NOT NULL,
mod INTEGER NOT NULL,
usn INTEGER NOT NULL,
type INTEGER NOT NULL,
queue INTEGER NOT NULL,
due INTEGER NOT NULL,
ivl INTEGER NOT NULL,
factor INTEGER NOT NULL,
reps INTEGER NOT NULL,
lapses INTEGER NOT NULL,
left INTEGER NOT NULL,
odue INTEGER NOT NULL,
odid INTEGER NOT NULL,
flags INTEGER NOT NULL,
data TEXT NOT NULL
)
`);
// Insert collection data
const decks = {
"1": { id: 1, name: "Default", desc: "" },
"1234567890123": {
id: 1234567890123,
name: "Test Deck",
desc: "A test deck",
},
};
const models = {
"9876543210987": {
id: 9876543210987,
name: "Basic",
flds: [{ name: "Front" }, { name: "Back" }],
tmpls: [{ name: "Card 1", qfmt: "{{Front}}", afmt: "{{Back}}" }],
},
};
const insertCol = db.prepare(`
INSERT INTO col (id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, dconf, tags)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
insertCol.run(
1,
1600000000,
1600000001000,
1600000000000,
11,
0,
-1,
0,
"{}",
JSON.stringify(models),
JSON.stringify(decks),
"{}",
"{}",
);
// Insert test notes
const insertNote = db.prepare(`
INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
// Note 1: Simple card
insertNote.run(
1000000000001,
"abc123",
9876543210987,
1600000001,
-1,
" vocabulary test ",
"Hello\x1fWorld",
"Hello",
12345,
0,
"",
);
// Note 2: Card with multiple tags
insertNote.run(
1000000000002,
"def456",
9876543210987,
1600000002,
-1,
" japanese kanji n5 ",
"日本語\x1fJapanese",
"日本語",
67890,
0,
"",
);
// Note 3: Card with no tags
insertNote.run(
1000000000003,
"ghi789",
9876543210987,
1600000003,
-1,
"",
"Question\x1fAnswer",
"Question",
11111,
0,
"",
);
// Insert test cards
const insertCard = db.prepare(`
INSERT INTO cards (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
// Card for note 1 (new card)
insertCard.run(
2000000000001,
1000000000001,
1234567890123,
0,
1600000001,
-1,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
"",
);
// Card for note 2 (review card)
insertCard.run(
2000000000002,
1000000000002,
1234567890123,
0,
1600000002,
-1,
2,
2,
100,
30,
2500,
5,
1,
0,
0,
0,
0,
"",
);
// Card for note 3 (learning card)
insertCard.run(
2000000000003,
1000000000003,
1234567890123,
0,
1600000003,
-1,
1,
1,
1600100000,
1,
2500,
1,
0,
1001,
0,
0,
0,
"",
);
db.close();
}
describe("Anki Parser", () => {
let tempDir: string;
let testApkgPath: string;
beforeAll(async () => {
// Create temp directory
tempDir = join(tmpdir(), `kioku-test-${randomBytes(8).toString("hex")}`);
await mkdir(tempDir, { recursive: true });
// Create test database
const dbPath = join(tempDir, "collection.anki2");
createTestAnkiDb(dbPath);
// Read the database file
const { readFile } = await import("node:fs/promises");
const dbBuffer = await readFile(dbPath);
// Create media file (empty JSON object)
const mediaBuffer = Buffer.from("{}", "utf8");
// Create ZIP with database and media
const zipEntries = new Map<string, Buffer>();
zipEntries.set("collection.anki2", dbBuffer);
zipEntries.set("media", mediaBuffer);
const zipBuffer = createZip(zipEntries);
// Write the .apkg file
testApkgPath = join(tempDir, "test.apkg");
await writeFile(testApkgPath, zipBuffer);
});
afterAll(async () => {
// Clean up
await rm(tempDir, { recursive: true, force: true });
});
describe("listAnkiPackageContents", () => {
it("should list files in the package", async () => {
const files = await listAnkiPackageContents(testApkgPath);
expect(files).toContain("collection.anki2");
expect(files).toContain("media");
});
});
describe("parseAnkiPackage", () => {
let result: AnkiPackage;
beforeAll(async () => {
result = await parseAnkiPackage(testApkgPath);
});
it("should parse decks correctly", () => {
expect(result.decks.length).toBe(2);
const testDeck = result.decks.find((d) => d.name === "Test Deck");
expect(testDeck).toBeDefined();
expect(testDeck?.description).toBe("A test deck");
const defaultDeck = result.decks.find((d) => d.name === "Default");
expect(defaultDeck).toBeDefined();
});
it("should parse models correctly", () => {
expect(result.models.length).toBe(1);
const basicModel = result.models[0];
expect(basicModel).toBeDefined();
expect(basicModel?.name).toBe("Basic");
expect(basicModel?.fields).toEqual(["Front", "Back"]);
expect(basicModel?.templates.length).toBe(1);
expect(basicModel?.templates[0]?.name).toBe("Card 1");
expect(basicModel?.templates[0]?.qfmt).toBe("{{Front}}");
expect(basicModel?.templates[0]?.afmt).toBe("{{Back}}");
});
it("should parse notes correctly", () => {
expect(result.notes.length).toBe(3);
// Note 1
const note1 = result.notes.find((n) => n.guid === "abc123");
expect(note1).toBeDefined();
expect(note1?.fields).toEqual(["Hello", "World"]);
expect(note1?.tags).toEqual(["vocabulary", "test"]);
expect(note1?.sfld).toBe("Hello");
// Note 2
const note2 = result.notes.find((n) => n.guid === "def456");
expect(note2).toBeDefined();
expect(note2?.fields).toEqual(["日本語", "Japanese"]);
expect(note2?.tags).toEqual(["japanese", "kanji", "n5"]);
// Note 3 (no tags)
const note3 = result.notes.find((n) => n.guid === "ghi789");
expect(note3).toBeDefined();
expect(note3?.tags).toEqual([]);
});
it("should parse cards correctly", () => {
expect(result.cards.length).toBe(3);
// New card
const card1 = result.cards.find((c) => c.nid === 1000000000001);
expect(card1).toBeDefined();
expect(card1?.type).toBe(0); // new
expect(card1?.reps).toBe(0);
// Review card
const card2 = result.cards.find((c) => c.nid === 1000000000002);
expect(card2).toBeDefined();
expect(card2?.type).toBe(2); // review
expect(card2?.ivl).toBe(30);
expect(card2?.reps).toBe(5);
expect(card2?.lapses).toBe(1);
// Learning card
const card3 = result.cards.find((c) => c.nid === 1000000000003);
expect(card3).toBeDefined();
expect(card3?.type).toBe(1); // learning
});
it("should throw error for non-existent file", async () => {
await expect(parseAnkiPackage("/non/existent/file.apkg")).rejects.toThrow(
"File not found",
);
});
it("should throw error for invalid package without database", async () => {
// Create a ZIP without a database
const zipEntries = new Map<string, Buffer>();
zipEntries.set("media", Buffer.from("{}", "utf8"));
const invalidZip = createZip(zipEntries);
const invalidPath = join(tempDir, "invalid.apkg");
await writeFile(invalidPath, invalidZip);
await expect(parseAnkiPackage(invalidPath)).rejects.toThrow(
"No Anki database found",
);
});
});
describe("ZIP extraction", () => {
it("should handle uncompressed entries", async () => {
// Create a ZIP with uncompressed entries
const name = Buffer.from("test.txt", "utf8");
const data = Buffer.from("Hello, World!", "utf8");
// Local file header (uncompressed)
const localHeader = Buffer.alloc(30 + name.length);
localHeader.writeUInt32LE(0x04034b50, 0);
localHeader.writeUInt16LE(20, 4);
localHeader.writeUInt16LE(0, 6);
localHeader.writeUInt16LE(0, 8); // no compression
localHeader.writeUInt16LE(0, 10);
localHeader.writeUInt16LE(0, 12);
localHeader.writeUInt32LE(0, 14);
localHeader.writeUInt32LE(data.length, 18);
localHeader.writeUInt32LE(data.length, 22);
localHeader.writeUInt16LE(name.length, 26);
localHeader.writeUInt16LE(0, 28);
name.copy(localHeader, 30);
// Central directory
const centralEntry = Buffer.alloc(46 + name.length);
centralEntry.writeUInt32LE(0x02014b50, 0);
centralEntry.writeUInt16LE(20, 4);
centralEntry.writeUInt16LE(20, 6);
centralEntry.writeUInt16LE(0, 8);
centralEntry.writeUInt16LE(0, 10);
centralEntry.writeUInt16LE(0, 12);
centralEntry.writeUInt16LE(0, 14);
centralEntry.writeUInt32LE(0, 16);
centralEntry.writeUInt32LE(data.length, 20);
centralEntry.writeUInt32LE(data.length, 24);
centralEntry.writeUInt16LE(name.length, 28);
centralEntry.writeUInt16LE(0, 30);
centralEntry.writeUInt16LE(0, 32);
centralEntry.writeUInt16LE(0, 34);
centralEntry.writeUInt16LE(0, 36);
centralEntry.writeUInt32LE(0, 38);
centralEntry.writeUInt32LE(0, 42);
name.copy(centralEntry, 46);
// End of central directory
const endRecord = Buffer.alloc(22);
endRecord.writeUInt32LE(0x06054b50, 0);
endRecord.writeUInt16LE(0, 4);
endRecord.writeUInt16LE(0, 6);
endRecord.writeUInt16LE(1, 8);
endRecord.writeUInt16LE(1, 10);
endRecord.writeUInt32LE(centralEntry.length, 12);
endRecord.writeUInt32LE(localHeader.length + data.length, 16);
endRecord.writeUInt16LE(0, 20);
const zipBuffer = Buffer.concat([
localHeader,
data,
centralEntry,
endRecord,
]);
const testPath = join(tempDir, "uncompressed.zip");
await writeFile(testPath, zipBuffer);
const files = await listAnkiPackageContents(testPath);
expect(files).toContain("test.txt");
});
});
describe("mapAnkiToKioku", () => {
let pkg: AnkiPackage;
beforeAll(async () => {
pkg = await parseAnkiPackage(testApkgPath);
});
it("should map decks correctly", () => {
const result = mapAnkiToKioku(pkg);
// Should have Test Deck (skipping Default deck by default)
expect(result.length).toBe(1);
const testDeckData = result.find((d) => d.deck.name === "Test Deck");
expect(testDeckData).toBeDefined();
expect(testDeckData?.deck.description).toBe("A test deck");
});
it("should include default deck when skipDefaultDeck is false", () => {
// Create a package with cards in both default and test decks
const pkgWithDefault: AnkiPackage = {
notes: [
{
id: 1,
guid: "test1",
mid: 1,
mod: 0,
tags: [],
fields: ["Front1", "Back1"],
sfld: "Front1",
},
{
id: 2,
guid: "test2",
mid: 1,
mod: 0,
tags: [],
fields: ["Front2", "Back2"],
sfld: "Front2",
},
],
cards: [
{
id: 1,
nid: 1,
did: 1, // Default deck
ord: 0,
mod: 0,
type: 0,
queue: 0,
due: 0,
ivl: 0,
factor: 0,
reps: 0,
lapses: 0,
},
{
id: 2,
nid: 2,
did: 2, // Test deck
ord: 0,
mod: 0,
type: 0,
queue: 0,
due: 0,
ivl: 0,
factor: 0,
reps: 0,
lapses: 0,
},
],
decks: [
{ id: 1, name: "Default", description: "" },
{ id: 2, name: "Test Deck", description: "" },
],
models: [
{
id: 1,
name: "Basic",
fields: ["Front", "Back"],
templates: [
{ name: "Card 1", qfmt: "{{Front}}", afmt: "{{Back}}" },
],
},
],
};
// With skipDefaultDeck = true (default), should only have Test Deck
const resultSkip = mapAnkiToKioku(pkgWithDefault);
expect(resultSkip.length).toBe(1);
expect(resultSkip[0]?.deck.name).toBe("Test Deck");
// With skipDefaultDeck = false, should have both decks
const result = mapAnkiToKioku(pkgWithDefault, { skipDefaultDeck: false });
const deckNames = result.map((d) => d.deck.name);
expect(deckNames).toContain("Default");
expect(deckNames).toContain("Test Deck");
});
it("should map note fields to front/back", () => {
const result = mapAnkiToKioku(pkg);
const testDeckData = result.find((d) => d.deck.name === "Test Deck");
expect(testDeckData?.cards.length).toBe(3);
// Card 1: Hello/World
const card1 = testDeckData?.cards.find((c) => c.front === "Hello");
expect(card1).toBeDefined();
expect(card1?.back).toBe("World");
// Card 2: 日本語/Japanese
const card2 = testDeckData?.cards.find((c) => c.front === "日本語");
expect(card2).toBeDefined();
expect(card2?.back).toBe("Japanese");
// Card 3: Question/Answer
const card3 = testDeckData?.cards.find((c) => c.front === "Question");
expect(card3).toBeDefined();
expect(card3?.back).toBe("Answer");
});
it("should map card states correctly", () => {
const result = mapAnkiToKioku(pkg);
const testDeckData = result.find((d) => d.deck.name === "Test Deck");
// New card (Hello/World)
const newCard = testDeckData?.cards.find((c) => c.front === "Hello");
expect(newCard?.state).toBe(0); // New
// Review card (日本語/Japanese)
const reviewCard = testDeckData?.cards.find((c) => c.front === "日本語");
expect(reviewCard?.state).toBe(2); // Review
expect(reviewCard?.reps).toBe(5);
expect(reviewCard?.lapses).toBe(1);
// Learning card (Question/Answer)
const learningCard = testDeckData?.cards.find(
(c) => c.front === "Question",
);
expect(learningCard?.state).toBe(1); // Learning
});
it("should map scheduling data correctly", () => {
const result = mapAnkiToKioku(pkg);
const testDeckData = result.find((d) => d.deck.name === "Test Deck");
// Review card has interval of 30 days
const reviewCard = testDeckData?.cards.find((c) => c.front === "日本語");
expect(reviewCard?.scheduledDays).toBe(30);
expect(reviewCard?.stability).toBe(30); // Stability approximates interval
expect(reviewCard?.elapsedDays).toBe(30);
// New card has no interval
const newCard = testDeckData?.cards.find((c) => c.front === "Hello");
expect(newCard?.scheduledDays).toBe(0);
expect(newCard?.stability).toBe(0);
expect(newCard?.elapsedDays).toBe(0);
});
it("should convert Anki factor to FSRS difficulty", () => {
const result = mapAnkiToKioku(pkg);
const testDeckData = result.find((d) => d.deck.name === "Test Deck");
// Review card has factor 2500 (default ease)
const reviewCard = testDeckData?.cards.find((c) => c.front === "日本語");
// Factor 2500 should map to a moderate difficulty (around 5)
expect(reviewCard?.difficulty).toBeGreaterThan(0);
expect(reviewCard?.difficulty).toBeLessThan(10);
// New card has factor 0, should have difficulty 0
const newCard = testDeckData?.cards.find((c) => c.front === "Hello");
expect(newCard?.difficulty).toBe(0);
});
it("should set due date for cards", () => {
const result = mapAnkiToKioku(pkg);
const testDeckData = result.find((d) => d.deck.name === "Test Deck");
// All cards should have valid due dates
for (const card of testDeckData?.cards || []) {
expect(card.due).toBeInstanceOf(Date);
expect(card.due.getTime()).not.toBeNaN();
}
});
it("should set lastReview for reviewed cards", () => {
const result = mapAnkiToKioku(pkg);
const testDeckData = result.find((d) => d.deck.name === "Test Deck");
// Review card has been reviewed
const reviewCard = testDeckData?.cards.find((c) => c.front === "日本語");
expect(reviewCard?.lastReview).toBeInstanceOf(Date);
// New card has not been reviewed
const newCard = testDeckData?.cards.find((c) => c.front === "Hello");
expect(newCard?.lastReview).toBeNull();
});
it("should strip HTML tags from fields", () => {
// Create a mock package with HTML in fields
const htmlPkg: AnkiPackage = {
notes: [
{
id: 1,
guid: "test1",
mid: 1,
mod: 0,
tags: [],
fields: ["<b>Bold</b> text", "<div>Answer</div><br/>Line 2"],
sfld: "Bold text",
},
],
cards: [
{
id: 1,
nid: 1,
did: 2,
ord: 0,
mod: 0,
type: 0,
queue: 0,
due: 0,
ivl: 0,
factor: 0,
reps: 0,
lapses: 0,
},
],
decks: [{ id: 2, name: "HTML Test", description: "" }],
models: [
{
id: 1,
name: "Basic",
fields: ["Front", "Back"],
templates: [
{ name: "Card 1", qfmt: "{{Front}}", afmt: "{{Back}}" },
],
},
],
};
const result = mapAnkiToKioku(htmlPkg);
const card = result[0]?.cards[0];
expect(card?.front).toBe("Bold text");
expect(card?.back).toBe("Answer\nLine 2");
});
it("should decode HTML entities", () => {
const htmlPkg: AnkiPackage = {
notes: [
{
id: 1,
guid: "test1",
mid: 1,
mod: 0,
tags: [],
fields: [
"<code> & "quotes"",
" spaced ",
],
sfld: "code",
},
],
cards: [
{
id: 1,
nid: 1,
did: 2,
ord: 0,
mod: 0,
type: 0,
queue: 0,
due: 0,
ivl: 0,
factor: 0,
reps: 0,
lapses: 0,
},
],
decks: [{ id: 2, name: "Entity Test", description: "" }],
models: [
{
id: 1,
name: "Basic",
fields: ["Front", "Back"],
templates: [
{ name: "Card 1", qfmt: "{{Front}}", afmt: "{{Back}}" },
],
},
],
};
const result = mapAnkiToKioku(htmlPkg);
const card = result[0]?.cards[0];
expect(card?.front).toBe('<code> & "quotes"');
expect(card?.back).toBe("spaced");
});
it("should handle empty package", () => {
const emptyPkg: AnkiPackage = {
notes: [],
cards: [],
decks: [],
models: [],
};
const result = mapAnkiToKioku(emptyPkg);
expect(result).toEqual([]);
});
it("should skip cards with missing notes", () => {
const incompletePkg: AnkiPackage = {
notes: [], // No notes
cards: [
{
id: 1,
nid: 999, // Non-existent note
did: 2,
ord: 0,
mod: 0,
type: 0,
queue: 0,
due: 0,
ivl: 0,
factor: 0,
reps: 0,
lapses: 0,
},
],
decks: [{ id: 2, name: "Test", description: "" }],
models: [],
};
const result = mapAnkiToKioku(incompletePkg);
// Should have deck but no cards
expect(result).toEqual([]);
});
});
});
|