@tryghost/mg-context 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -125,14 +125,12 @@ describe('MigrateContext', () => {
125
125
  post2.set('created_at', new Date('2023-11-24T12:00:00.000Z'));
126
126
  await post2.save(instance.db);
127
127
  const progressCalls = [];
128
+ instance.on('progress', (_event, processed, total) => {
129
+ progressCalls.push([processed, total]);
130
+ });
128
131
  await instance.forEachPost(async (post) => {
129
132
  post.set('status', 'published');
130
- }, {
131
- batchSize: 1,
132
- progress(processed, total) {
133
- progressCalls.push([processed, total]);
134
- }
135
- });
133
+ }, { batchSize: 1 });
136
134
  const allPosts = await instance.getAllPosts();
137
135
  assert.equal(allPosts[0].data.status, 'published');
138
136
  assert.equal(allPosts[1].data.status, 'published');
@@ -397,12 +395,10 @@ describe('MigrateContext', () => {
397
395
  await post.save(instance.db);
398
396
  }
399
397
  const progressCalls = [];
400
- await instance.forEachGhostPost(async () => { }, {
401
- batchSize: 2,
402
- progress(processed, total) {
403
- progressCalls.push([processed, total]);
404
- }
398
+ instance.on('progress', (_event, processed, total) => {
399
+ progressCalls.push([processed, total]);
405
400
  });
401
+ await instance.forEachGhostPost(async () => { }, { batchSize: 2 });
406
402
  assert.deepEqual(progressCalls, [[2, 3], [3, 3]]);
407
403
  await instance.close();
408
404
  });
@@ -644,6 +640,7 @@ describe('MigrateContext', () => {
644
640
  post.set('html', '<p>Hello world</p>');
645
641
  post.addAuthor({ name: 'Test Author', slug: 'test-author', email: 'test@example.com' });
646
642
  await post.save(instance.db);
643
+ await instance.prepareForExport();
647
644
  const writtenFiles = await instance.writeGhostJson(tmpdir(), { filename: 'lexical-test' });
648
645
  const ghostJSON = JSON.parse(await readFile(writtenFiles[0].path, 'utf-8'));
649
646
  assert.equal(ghostJSON.data.posts.length, 1);
@@ -665,6 +662,7 @@ describe('MigrateContext', () => {
665
662
  post.set('html', '<p>Hello world</p>');
666
663
  post.addAuthor({ name: 'Test Author', slug: 'test-author', email: 'test@example.com' });
667
664
  await post.save(instance.db);
665
+ await instance.prepareForExport();
668
666
  const writtenFiles = await instance.writeGhostJson(tmpdir(), { filename: 'mobiledoc-test' });
669
667
  const ghostJSON = JSON.parse(await readFile(writtenFiles[0].path, 'utf-8'));
670
668
  assert.equal(ghostJSON.data.posts.length, 1);
@@ -934,7 +932,7 @@ describe('MigrateContext', () => {
934
932
  await post.save(instance.db);
935
933
  assert.throws(() => {
936
934
  withTransaction(instance.db, () => {
937
- instance.db.stmts.updatePost.run(JSON.stringify({ title: 'Rolled Back', slug: 'before-rollback' }), '{}', '{}', 'lexical', null, post.ghostId, '2023-11-23T00:00:00.000Z', null, null, post.dbId);
935
+ instance.db.stmts.updatePost.run(JSON.stringify({ title: 'Rolled Back', slug: 'before-rollback' }), '{}', '{}', 'lexical', null, post.ghostId, '2023-11-23T00:00:00.000Z', null, null, 'before-rollback', post.dbId);
938
936
  throw new errors.InternalServerError({ message: 'sync rollback' });
939
937
  });
940
938
  }, { message: 'sync rollback' });
@@ -953,7 +951,7 @@ describe('MigrateContext', () => {
953
951
  // Outer transaction via ctx.transaction, inner via withTransaction
954
952
  await instance.transaction(async () => {
955
953
  withTransaction(instance.db, () => {
956
- instance.db.stmts.updatePost.run(JSON.stringify({ title: 'Updated In Nested', slug: 'nested-tx' }), '{}', '{}', 'lexical', null, post.ghostId, '2023-11-23T00:00:00.000Z', null, null, post.dbId);
954
+ instance.db.stmts.updatePost.run(JSON.stringify({ title: 'Updated In Nested', slug: 'nested-tx' }), '{}', '{}', 'lexical', null, post.ghostId, '2023-11-23T00:00:00.000Z', null, null, 'nested-tx', post.dbId);
957
955
  });
958
956
  });
959
957
  const allPosts = await instance.getAllPosts();
@@ -1127,7 +1125,7 @@ describe('MigrateContext', () => {
1127
1125
  });
1128
1126
  });
1129
1127
  describe('Conversion caching', () => {
1130
- it('writeGhostJson caches converted content back to DB', async () => {
1128
+ it('prepareForExport converts and caches content in DB', async () => {
1131
1129
  const instance = new MigrateContext();
1132
1130
  await instance.init();
1133
1131
  const post = await instance.addPost();
@@ -1136,22 +1134,19 @@ describe('MigrateContext', () => {
1136
1134
  post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1137
1135
  post.set('html', '<p>Hello</p>');
1138
1136
  await post.save(instance.db);
1139
- // Before write: no cached lexical
1137
+ // Before: no cached lexical
1140
1138
  const rowBefore = instance.db.stmts.findPostById.get(post.dbId);
1141
1139
  assert.equal(JSON.parse(rowBefore.data).lexical, null);
1142
- // Write JSON — converts and caches
1143
- const dir = join(tmpdir(), `mg-cache-${Date.now()}`);
1144
- await instance.writeGhostJson(dir);
1145
- // After write: lexical cached in DB
1140
+ await instance.prepareForExport();
1141
+ // After: lexical cached in DB
1146
1142
  const rowAfter = instance.db.stmts.findPostById.get(post.dbId);
1147
1143
  const stored = JSON.parse(rowAfter.data);
1148
- assert.ok(stored.lexical, 'lexical should be cached after writeGhostJson');
1144
+ assert.ok(stored.lexical, 'lexical should be cached after prepareForExport');
1149
1145
  assert.ok(JSON.parse(stored.lexical).root);
1150
1146
  assert.equal(stored.html, '<p>Hello</p>', 'html should be preserved');
1151
- await rm(dir, { recursive: true, force: true });
1152
1147
  await instance.close();
1153
1148
  });
1154
- it('Second writeGhostJson skips conversion (uses cache)', async () => {
1149
+ it('Skips posts that already have converted content', async () => {
1155
1150
  const instance = new MigrateContext();
1156
1151
  await instance.init();
1157
1152
  const post = await instance.addPost();
@@ -1161,17 +1156,18 @@ describe('MigrateContext', () => {
1161
1156
  post.set('html', '<p>Hello</p>');
1162
1157
  await post.save(instance.db);
1163
1158
  const dir = join(tmpdir(), `mg-skip-${Date.now()}`);
1164
- // First write: converts
1159
+ await instance.prepareForExport();
1165
1160
  await instance.writeGhostJson(dir);
1166
1161
  const file1 = JSON.parse(await readFile(join(dir, 'posts.json'), 'utf-8'));
1167
- // Second write: uses cached content (same output)
1162
+ // Second prepare + write: uses cached content (same output)
1163
+ await instance.prepareForExport();
1168
1164
  await instance.writeGhostJson(dir);
1169
1165
  const file2 = JSON.parse(await readFile(join(dir, 'posts.json'), 'utf-8'));
1170
1166
  assert.equal(file1.data.posts[0].lexical, file2.data.posts[0].lexical);
1171
1167
  await rm(dir, { recursive: true, force: true });
1172
1168
  await instance.close();
1173
1169
  });
1174
- it('Cache is invalidated when HTML changes via forEachPost', async () => {
1170
+ it('Re-converts after HTML changes via forEachPost', async () => {
1175
1171
  const instance = new MigrateContext();
1176
1172
  await instance.init();
1177
1173
  const post = await instance.addPost();
@@ -1180,9 +1176,8 @@ describe('MigrateContext', () => {
1180
1176
  post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1181
1177
  post.set('html', '<p>Original</p>');
1182
1178
  await post.save(instance.db);
1183
- const dir = join(tmpdir(), `mg-invalidate-${Date.now()}`);
1184
- // First write: converts and caches
1185
- await instance.writeGhostJson(dir);
1179
+ // First prepare: converts and caches
1180
+ await instance.prepareForExport();
1186
1181
  const row1 = instance.db.stmts.findPostById.get(post.dbId);
1187
1182
  const lexical1 = JSON.parse(row1.data).lexical;
1188
1183
  assert.ok(lexical1);
@@ -1192,13 +1187,12 @@ describe('MigrateContext', () => {
1192
1187
  });
1193
1188
  const rowAfterUpdate = instance.db.stmts.findPostById.get(post.dbId);
1194
1189
  assert.equal(JSON.parse(rowAfterUpdate.data).lexical, null, 'lexical should be cleared after HTML change');
1195
- // Second write: re-converts with new HTML
1196
- await instance.writeGhostJson(dir);
1190
+ // Second prepare: re-converts with new HTML
1191
+ await instance.prepareForExport();
1197
1192
  const row2 = instance.db.stmts.findPostById.get(post.dbId);
1198
1193
  const lexical2 = JSON.parse(row2.data).lexical;
1199
1194
  assert.ok(lexical2);
1200
1195
  assert.notEqual(lexical1, lexical2);
1201
- await rm(dir, { recursive: true, force: true });
1202
1196
  await instance.close();
1203
1197
  });
1204
1198
  it('set html invalidates cached conversion', async () => {
@@ -1217,7 +1211,7 @@ describe('MigrateContext', () => {
1217
1211
  assert.equal(post.htmlDirty, true);
1218
1212
  await instance.close();
1219
1213
  });
1220
- it('Mobiledoc conversion is cached', async () => {
1214
+ it('Mobiledoc conversion is cached by prepareForExport', async () => {
1221
1215
  const instance = new MigrateContext({ contentFormat: 'mobiledoc' });
1222
1216
  await instance.init();
1223
1217
  const post = await instance.addPost();
@@ -1226,14 +1220,29 @@ describe('MigrateContext', () => {
1226
1220
  post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1227
1221
  post.set('html', '<p>Mobiledoc</p>');
1228
1222
  await post.save(instance.db);
1229
- const dir = join(tmpdir(), `mg-mobiledoc-cache-${Date.now()}`);
1230
- await instance.writeGhostJson(dir);
1223
+ await instance.prepareForExport();
1231
1224
  const row = instance.db.stmts.findPostById.get(post.dbId);
1232
1225
  const stored = JSON.parse(row.data);
1233
1226
  assert.ok(stored.mobiledoc);
1234
1227
  assert.ok(JSON.parse(stored.mobiledoc).version);
1235
1228
  assert.equal(stored.lexical, null);
1236
- await rm(dir, { recursive: true, force: true });
1229
+ await instance.close();
1230
+ });
1231
+ it('Skips html-format posts', async () => {
1232
+ const instance = new MigrateContext({ contentFormat: 'html' });
1233
+ await instance.init();
1234
+ const post = await instance.addPost();
1235
+ post.set('title', 'HTML Only');
1236
+ post.set('slug', 'html-only');
1237
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1238
+ post.set('html', '<p>Hello</p>');
1239
+ await post.save(instance.db);
1240
+ await instance.prepareForExport();
1241
+ const row = instance.db.stmts.findPostById.get(post.dbId);
1242
+ const stored = JSON.parse(row.data);
1243
+ assert.equal(stored.html, '<p>Hello</p>');
1244
+ assert.equal(stored.lexical, null);
1245
+ assert.equal(stored.mobiledoc, null);
1237
1246
  await instance.close();
1238
1247
  });
1239
1248
  it('convertContent can be called manually', async () => {
@@ -1253,6 +1262,88 @@ describe('MigrateContext', () => {
1253
1262
  assert.ok(stored.lexical);
1254
1263
  await instance.close();
1255
1264
  });
1265
+ it('Reports progress during conversion', async () => {
1266
+ const instance = new MigrateContext();
1267
+ await instance.init();
1268
+ for (let i = 0; i < 3; i += 1) {
1269
+ const post = await instance.addPost();
1270
+ post.set('title', `Post ${i}`);
1271
+ post.set('slug', `post-${i}`);
1272
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1273
+ post.set('html', `<p>Content ${i}</p>`);
1274
+ await post.save(instance.db);
1275
+ }
1276
+ const calls = [];
1277
+ instance.on('progress', (_event, processed, total) => {
1278
+ calls.push([processed, total]);
1279
+ });
1280
+ await instance.prepareForExport();
1281
+ assert.ok(calls.length > 0);
1282
+ const last = calls[calls.length - 1];
1283
+ assert.equal(last[0], 3);
1284
+ assert.equal(last[1], 3);
1285
+ await instance.close();
1286
+ });
1287
+ it('Can remove progress listener with off', async () => {
1288
+ const instance = new MigrateContext();
1289
+ await instance.init();
1290
+ const post = await instance.addPost();
1291
+ post.set('title', 'Off Test');
1292
+ post.set('slug', 'off-test');
1293
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1294
+ post.set('html', '<p>Hello</p>');
1295
+ await post.save(instance.db);
1296
+ const calls = [];
1297
+ const listener = (_event, processed) => {
1298
+ calls.push(processed);
1299
+ };
1300
+ instance.on('progress', listener);
1301
+ instance.off('progress', listener);
1302
+ await instance.prepareForExport();
1303
+ assert.equal(calls.length, 0);
1304
+ await instance.close();
1305
+ });
1306
+ it('emitEvents: false suppresses all progress events', async () => {
1307
+ const instance = new MigrateContext({ emitEvents: false });
1308
+ await instance.init();
1309
+ const post = await instance.addPost();
1310
+ post.set('title', 'Silent');
1311
+ post.set('slug', 'silent');
1312
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1313
+ post.set('html', '<p>Hello</p>');
1314
+ await post.save(instance.db);
1315
+ const calls = [];
1316
+ instance.on('progress', (...args) => {
1317
+ calls.push(args);
1318
+ });
1319
+ await instance.prepareForExport();
1320
+ await instance.forEachPost(async () => { });
1321
+ assert.equal(calls.length, 0);
1322
+ await instance.close();
1323
+ });
1324
+ it('Deduplicates slugs at insert time and reports via prepareForExport', async () => {
1325
+ const instance = new MigrateContext();
1326
+ await instance.init();
1327
+ const post1 = await instance.addPost();
1328
+ post1.set('title', 'First');
1329
+ post1.set('slug', 'same');
1330
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1331
+ post1.set('published_at', new Date('2023-01-01T00:00:00.000Z'));
1332
+ await post1.save(instance.db);
1333
+ const post2 = await instance.addPost();
1334
+ post2.set('title', 'Second');
1335
+ post2.set('slug', 'same');
1336
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
1337
+ post2.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
1338
+ await post2.save(instance.db);
1339
+ // Slug was already deduped at insert time
1340
+ assert.equal(post2.data.slug, 'same-2');
1341
+ await instance.prepareForExport();
1342
+ assert.equal(instance.duplicateSlugs.length, 2);
1343
+ assert.equal(instance.duplicateSlugs[0].newSlug, 'same');
1344
+ assert.equal(instance.duplicateSlugs[1].newSlug, 'same-2');
1345
+ await instance.close();
1346
+ });
1256
1347
  });
1257
1348
  describe('PostFilter', () => {
1258
1349
  let ctx;
@@ -1410,12 +1501,12 @@ describe('MigrateContext', () => {
1410
1501
  });
1411
1502
  it('Progress reports correct filtered total', async () => {
1412
1503
  const progressCalls = [];
1504
+ ctx.on('progress', (_event, processed, total) => {
1505
+ progressCalls.push([processed, total]);
1506
+ });
1413
1507
  // eslint-disable-next-line no-unused-vars
1414
1508
  await ctx.forEachPost(async (_post) => { }, {
1415
- filter: { tag: { slug: 'news' } },
1416
- progress(processed, total) {
1417
- progressCalls.push([processed, total]);
1418
- }
1509
+ filter: { tag: { slug: 'news' } }
1419
1510
  });
1420
1511
  assert.ok(progressCalls.length > 0);
1421
1512
  const lastCall = progressCalls[progressCalls.length - 1];
@@ -1444,12 +1535,12 @@ describe('MigrateContext', () => {
1444
1535
  });
1445
1536
  it('Tag filter with progress reports correct total', async () => {
1446
1537
  const progressCalls = [];
1538
+ ctx.on('progress', (_event, processed, total) => {
1539
+ progressCalls.push([processed, total]);
1540
+ });
1447
1541
  // eslint-disable-next-line no-unused-vars
1448
1542
  await ctx.forEachGhostPost(async (_json, _post) => { }, {
1449
- filter: { tag: { slug: 'news' } },
1450
- progress(processed, total) {
1451
- progressCalls.push([processed, total]);
1452
- }
1543
+ filter: { tag: { slug: 'news' } }
1453
1544
  });
1454
1545
  assert.ok(progressCalls.length > 0);
1455
1546
  const lastCall = progressCalls[progressCalls.length - 1];
@@ -1743,5 +1834,285 @@ describe('MigrateContext', () => {
1743
1834
  await ctx.close();
1744
1835
  });
1745
1836
  });
1837
+ describe('deduplicateSlugs', () => {
1838
+ it('Returns empty array when no duplicates exist', async () => {
1839
+ const instance = new MigrateContext();
1840
+ await instance.init();
1841
+ const post1 = await instance.addPost();
1842
+ post1.set('title', 'Post A');
1843
+ post1.set('slug', 'post-a');
1844
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1845
+ await post1.save(instance.db);
1846
+ const post2 = await instance.addPost();
1847
+ post2.set('title', 'Post B');
1848
+ post2.set('slug', 'post-b');
1849
+ post2.set('created_at', new Date('2023-01-02T00:00:00.000Z'));
1850
+ await post2.save(instance.db);
1851
+ const duplicates = await instance.deduplicateSlugs();
1852
+ assert.equal(duplicates.length, 0);
1853
+ await instance.close();
1854
+ });
1855
+ it('Deduplicates 2 posts with the same slug, oldest keeps original', async () => {
1856
+ const instance = new MigrateContext();
1857
+ await instance.init();
1858
+ const post1 = await instance.addPost({ source: { url: 'https://example.com/blog/my-post' } });
1859
+ post1.set('title', 'Older Post');
1860
+ post1.set('slug', 'my-post');
1861
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1862
+ post1.set('published_at', new Date('2023-01-01T00:00:00.000Z'));
1863
+ await post1.save(instance.db);
1864
+ const post2 = await instance.addPost({ source: { url: 'https://example.com/news/my-post' } });
1865
+ post2.set('title', 'Newer Post');
1866
+ post2.set('slug', 'my-post');
1867
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
1868
+ post2.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
1869
+ await post2.save(instance.db);
1870
+ const duplicates = await instance.deduplicateSlugs();
1871
+ // Includes retained post + renamed post
1872
+ assert.equal(duplicates.length, 2);
1873
+ assert.equal(duplicates[0].oldSlug, 'my-post');
1874
+ assert.equal(duplicates[0].newSlug, 'my-post');
1875
+ assert.equal(duplicates[0].url, 'https://example.com/blog/my-post');
1876
+ assert.equal(duplicates[1].oldSlug, 'my-post');
1877
+ assert.equal(duplicates[1].newSlug, 'my-post-2');
1878
+ assert.equal(duplicates[1].url, 'https://example.com/news/my-post');
1879
+ // Verify DB was updated
1880
+ const posts = await instance.findPosts({ slug: 'my-post' });
1881
+ assert.equal(posts.length, 1);
1882
+ assert.equal(posts[0].data.title, 'Older Post');
1883
+ const renamedPosts = await instance.findPosts({ slug: 'my-post-2' });
1884
+ assert.equal(renamedPosts.length, 1);
1885
+ assert.equal(renamedPosts[0].data.title, 'Newer Post');
1886
+ await instance.close();
1887
+ });
1888
+ it('Deduplicates 3 posts: oldest unchanged, next gets -2, newest gets -3', async () => {
1889
+ const instance = new MigrateContext();
1890
+ await instance.init();
1891
+ const post1 = await instance.addPost();
1892
+ post1.set('title', 'Oldest');
1893
+ post1.set('slug', 'my-post');
1894
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1895
+ post1.set('published_at', new Date('2023-01-01T00:00:00.000Z'));
1896
+ await post1.save(instance.db);
1897
+ const post2 = await instance.addPost();
1898
+ post2.set('title', 'Middle');
1899
+ post2.set('slug', 'my-post');
1900
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
1901
+ post2.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
1902
+ await post2.save(instance.db);
1903
+ const post3 = await instance.addPost();
1904
+ post3.set('title', 'Newest');
1905
+ post3.set('slug', 'my-post');
1906
+ post3.set('created_at', new Date('2023-12-01T00:00:00.000Z'));
1907
+ post3.set('published_at', new Date('2023-12-01T00:00:00.000Z'));
1908
+ await post3.save(instance.db);
1909
+ const duplicates = await instance.deduplicateSlugs();
1910
+ // Retained + 2 renamed = 3 entries, grouped together
1911
+ assert.equal(duplicates.length, 3);
1912
+ assert.equal(duplicates[0].newSlug, 'my-post');
1913
+ assert.equal(duplicates[1].newSlug, 'my-post-2');
1914
+ assert.equal(duplicates[2].newSlug, 'my-post-3');
1915
+ const oldest = await instance.findPosts({ slug: 'my-post' });
1916
+ assert.equal(oldest.length, 1);
1917
+ assert.equal(oldest[0].data.title, 'Oldest');
1918
+ await instance.close();
1919
+ });
1920
+ it('Skips to -3 when -2 slug already exists', async () => {
1921
+ const instance = new MigrateContext();
1922
+ await instance.init();
1923
+ // A post that naturally has the slug "my-post-2"
1924
+ const existing = await instance.addPost();
1925
+ existing.set('title', 'Existing -2');
1926
+ existing.set('slug', 'my-post-2');
1927
+ existing.set('created_at', new Date('2022-01-01T00:00:00.000Z'));
1928
+ existing.set('published_at', new Date('2022-01-01T00:00:00.000Z'));
1929
+ await existing.save(instance.db);
1930
+ const post1 = await instance.addPost();
1931
+ post1.set('title', 'Original');
1932
+ post1.set('slug', 'my-post');
1933
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1934
+ post1.set('published_at', new Date('2023-01-01T00:00:00.000Z'));
1935
+ await post1.save(instance.db);
1936
+ const post2 = await instance.addPost();
1937
+ post2.set('title', 'Duplicate');
1938
+ post2.set('slug', 'my-post');
1939
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
1940
+ post2.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
1941
+ await post2.save(instance.db);
1942
+ const duplicates = await instance.deduplicateSlugs();
1943
+ // Retained + 1 renamed = 2 entries
1944
+ assert.equal(duplicates.length, 2);
1945
+ assert.equal(duplicates[0].newSlug, 'my-post');
1946
+ assert.equal(duplicates[1].newSlug, 'my-post-3');
1947
+ // Verify the existing -2 post was not touched
1948
+ const existingPosts = await instance.findPosts({ slug: 'my-post-2' });
1949
+ assert.equal(existingPosts.length, 1);
1950
+ assert.equal(existingPosts[0].data.title, 'Existing -2');
1951
+ await instance.close();
1952
+ });
1953
+ it('Includes source URL for both retained and renamed entries', async () => {
1954
+ const instance = new MigrateContext();
1955
+ await instance.init();
1956
+ const post1 = await instance.addPost({ source: { url: 'https://example.com/blog/my-post' } });
1957
+ post1.set('title', 'First');
1958
+ post1.set('slug', 'my-post');
1959
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1960
+ post1.set('published_at', new Date('2023-01-01T00:00:00.000Z'));
1961
+ await post1.save(instance.db);
1962
+ const post2 = await instance.addPost({ source: { url: 'https://example.com/news/my-post' } });
1963
+ post2.set('title', 'Second');
1964
+ post2.set('slug', 'my-post');
1965
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
1966
+ post2.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
1967
+ await post2.save(instance.db);
1968
+ const duplicates = await instance.deduplicateSlugs();
1969
+ assert.equal(duplicates.length, 2);
1970
+ assert.equal(duplicates[0].url, 'https://example.com/blog/my-post');
1971
+ assert.equal(duplicates[1].url, 'https://example.com/news/my-post');
1972
+ await instance.close();
1973
+ });
1974
+ it('Falls back to canonical_url when source.url is absent', async () => {
1975
+ const instance = new MigrateContext();
1976
+ await instance.init();
1977
+ const post1 = await instance.addPost();
1978
+ post1.set('title', 'First');
1979
+ post1.set('slug', 'my-post');
1980
+ post1.set('canonical_url', 'https://example.com/canonical-1');
1981
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1982
+ post1.set('published_at', new Date('2023-01-01T00:00:00.000Z'));
1983
+ await post1.save(instance.db);
1984
+ const post2 = await instance.addPost();
1985
+ post2.set('title', 'Second');
1986
+ post2.set('slug', 'my-post');
1987
+ post2.set('canonical_url', 'https://example.com/canonical-2');
1988
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
1989
+ post2.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
1990
+ await post2.save(instance.db);
1991
+ const duplicates = await instance.deduplicateSlugs();
1992
+ assert.equal(duplicates[0].url, 'https://example.com/canonical-1');
1993
+ assert.equal(duplicates[1].url, 'https://example.com/canonical-2');
1994
+ await instance.close();
1995
+ });
1996
+ it('Uses created_at when published_at is null', async () => {
1997
+ const instance = new MigrateContext();
1998
+ await instance.init();
1999
+ // Draft (no published_at) created earlier should keep the slug
2000
+ const draft = await instance.addPost();
2001
+ draft.set('title', 'Old Draft');
2002
+ draft.set('slug', 'my-post');
2003
+ draft.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2004
+ await draft.save(instance.db);
2005
+ const published = await instance.addPost();
2006
+ published.set('title', 'Published Later');
2007
+ published.set('slug', 'my-post');
2008
+ published.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2009
+ published.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
2010
+ await published.save(instance.db);
2011
+ const duplicates = await instance.deduplicateSlugs();
2012
+ assert.equal(duplicates.length, 2);
2013
+ assert.equal(duplicates[0].newSlug, 'my-post');
2014
+ assert.equal(duplicates[1].newSlug, 'my-post-2');
2015
+ const kept = await instance.findPosts({ slug: 'my-post' });
2016
+ assert.equal(kept[0].data.title, 'Old Draft');
2017
+ await instance.close();
2018
+ });
2019
+ it('Handles multiple different duplicate slug groups', async () => {
2020
+ const instance = new MigrateContext();
2021
+ await instance.init();
2022
+ for (const [slug, title, date] of [
2023
+ ['alpha', 'Alpha 1', '2023-01-01'],
2024
+ ['alpha', 'Alpha 2', '2023-06-01'],
2025
+ ['beta', 'Beta 1', '2023-02-01'],
2026
+ ['beta', 'Beta 2', '2023-07-01'],
2027
+ ['beta', 'Beta 3', '2023-12-01'],
2028
+ ['gamma', 'Gamma Only', '2023-03-01']
2029
+ ]) {
2030
+ const post = await instance.addPost();
2031
+ post.set('title', title);
2032
+ post.set('slug', slug);
2033
+ post.set('created_at', new Date(`${date}T00:00:00.000Z`));
2034
+ post.set('published_at', new Date(`${date}T00:00:00.000Z`));
2035
+ await post.save(instance.db);
2036
+ }
2037
+ const duplicates = await instance.deduplicateSlugs();
2038
+ // alpha: retained + 1 renamed = 2, beta: retained + 2 renamed = 3, total = 5
2039
+ assert.equal(duplicates.length, 5);
2040
+ const alphas = duplicates.filter((d) => d.oldSlug === 'alpha');
2041
+ assert.equal(alphas.length, 2);
2042
+ assert.equal(alphas[0].newSlug, 'alpha');
2043
+ assert.equal(alphas[1].newSlug, 'alpha-2');
2044
+ const betas = duplicates.filter((d) => d.oldSlug === 'beta');
2045
+ assert.equal(betas.length, 3);
2046
+ assert.equal(betas[0].newSlug, 'beta');
2047
+ assert.equal(betas[1].newSlug, 'beta-2');
2048
+ assert.equal(betas[2].newSlug, 'beta-3');
2049
+ // gamma should not appear
2050
+ const gammas = duplicates.filter((d) => d.oldSlug === 'gamma');
2051
+ assert.equal(gammas.length, 0);
2052
+ // Verify grouping: all alpha entries come before all beta entries
2053
+ const lastAlphaIdx = duplicates.findLastIndex((d) => d.oldSlug === 'alpha');
2054
+ const firstBetaIdx = duplicates.findIndex((d) => d.oldSlug === 'beta');
2055
+ assert.ok(lastAlphaIdx < firstBetaIdx, 'alpha group should come before beta group');
2056
+ await instance.close();
2057
+ });
2058
+ it('prepareForExport deduplicates and results are used by writeGhostJson', async () => {
2059
+ const instance = new MigrateContext();
2060
+ await instance.init();
2061
+ const post1 = await instance.addPost({ source: { url: 'https://example.com/first' } });
2062
+ post1.set('title', 'First');
2063
+ post1.set('slug', 'dupe');
2064
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2065
+ post1.set('published_at', new Date('2023-01-01T00:00:00.000Z'));
2066
+ await post1.save(instance.db);
2067
+ const post2 = await instance.addPost({ source: { url: 'https://example.com/second' } });
2068
+ post2.set('title', 'Second');
2069
+ post2.set('slug', 'dupe');
2070
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2071
+ post2.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
2072
+ post2.addAuthor({ name: 'Test', slug: 'test', email: 'test@example.com' });
2073
+ await post2.save(instance.db);
2074
+ await instance.prepareForExport();
2075
+ const files = await instance.writeGhostJson(tmpdir(), { filename: 'dedup-test' });
2076
+ const content = JSON.parse(await readFile(files[0].path, 'utf-8'));
2077
+ const slugs = content.data.posts.map((p) => p.slug);
2078
+ assert.ok(slugs.includes('dupe'));
2079
+ assert.ok(slugs.includes('dupe-2'));
2080
+ // Results accessible via getter — includes retained + renamed
2081
+ assert.equal(instance.duplicateSlugs.length, 2);
2082
+ assert.equal(instance.duplicateSlugs[0].oldSlug, 'dupe');
2083
+ assert.equal(instance.duplicateSlugs[0].newSlug, 'dupe');
2084
+ assert.equal(instance.duplicateSlugs[0].url, 'https://example.com/first');
2085
+ assert.equal(instance.duplicateSlugs[1].newSlug, 'dupe-2');
2086
+ assert.equal(instance.duplicateSlugs[1].url, 'https://example.com/second');
2087
+ await unlink(files[0].path);
2088
+ await instance.close();
2089
+ });
2090
+ it('Only runs once even when called multiple times', async () => {
2091
+ const instance = new MigrateContext();
2092
+ await instance.init();
2093
+ const post1 = await instance.addPost();
2094
+ post1.set('title', 'First');
2095
+ post1.set('slug', 'once');
2096
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2097
+ post1.set('published_at', new Date('2023-01-01T00:00:00.000Z'));
2098
+ await post1.save(instance.db);
2099
+ const post2 = await instance.addPost();
2100
+ post2.set('title', 'Second');
2101
+ post2.set('slug', 'once');
2102
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2103
+ post2.set('published_at', new Date('2023-06-01T00:00:00.000Z'));
2104
+ await post2.save(instance.db);
2105
+ const first = await instance.deduplicateSlugs();
2106
+ assert.equal(first.length, 2);
2107
+ // Second call returns cached result without modifying anything
2108
+ const second = await instance.deduplicateSlugs();
2109
+ assert.equal(second.length, 2);
2110
+ assert.deepEqual(first, second);
2111
+ // Slug wasn't double-renamed
2112
+ const posts = await instance.findPosts({ slug: 'once-2' });
2113
+ assert.equal(posts.length, 1);
2114
+ await instance.close();
2115
+ });
2116
+ });
1746
2117
  });
1747
2118
  //# sourceMappingURL=migrate-context.test.js.map