@tryghost/mg-context 0.11.3 → 0.11.5

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.
@@ -820,6 +820,44 @@ describe('MigrateContext', () => {
820
820
  assert.equal(post2.dbId, post1.dbId);
821
821
  await instance.close();
822
822
  });
823
+ it('Does not record a slug rename for a post skipped by lookup_key', async () => {
824
+ const instance = new MigrateContext();
825
+ await instance.init();
826
+ const post1 = await instance.addPost({ lookupKey: 'https://example.com/post-1' });
827
+ post1.set('title', 'First Post');
828
+ post1.set('slug', 'same-slug');
829
+ post1.set('created_at', new Date('2023-11-23T12:00:00.000Z'));
830
+ await post1.save(instance.db);
831
+ const post2 = await instance.addPost({ lookupKey: 'https://example.com/post-1' });
832
+ post2.set('title', 'Duplicate Post');
833
+ post2.set('slug', 'same-slug');
834
+ post2.set('created_at', new Date('2023-11-24T12:00:00.000Z'));
835
+ await post2.save(instance.db);
836
+ // The skipped post never claimed a slug, so no rename was recorded
837
+ assert.equal(instance.db.slugRenames.length, 0);
838
+ // ...and it still holds the slug it was given
839
+ assert.equal(post2.data.slug, 'same-slug');
840
+ const duplicates = await instance.deduplicateSlugs();
841
+ assert.equal(duplicates.length, 0);
842
+ await instance.close();
843
+ });
844
+ it('Still deduplicates slugs across posts with different lookup_keys', async () => {
845
+ const instance = new MigrateContext();
846
+ await instance.init();
847
+ const post1 = await instance.addPost({ lookupKey: 'https://example.com/post-1' });
848
+ post1.set('title', 'First Post');
849
+ post1.set('slug', 'same-slug');
850
+ post1.set('created_at', new Date('2023-11-23T12:00:00.000Z'));
851
+ await post1.save(instance.db);
852
+ const post2 = await instance.addPost({ lookupKey: 'https://example.com/post-2' });
853
+ post2.set('title', 'Second Post');
854
+ post2.set('slug', 'same-slug');
855
+ post2.set('created_at', new Date('2023-11-24T12:00:00.000Z'));
856
+ await post2.save(instance.db);
857
+ assert.equal(instance.db.slugRenames.length, 1);
858
+ assert.equal(post2.data.slug, 'same-slug-2');
859
+ await instance.close();
860
+ });
823
861
  it('Posts without lookup_key always insert normally', async () => {
824
862
  const instance = new MigrateContext();
825
863
  await instance.init();
@@ -1942,6 +1980,38 @@ describe('MigrateContext', () => {
1942
1980
  await ctx.close();
1943
1981
  });
1944
1982
  });
1983
+ describe('User roles', () => {
1984
+ it('Exports roles as an array in the full Ghost JSON', async () => {
1985
+ const ctx = new MigrateContext();
1986
+ await ctx.init();
1987
+ const post = await ctx.addPost();
1988
+ post.set('title', 'Test');
1989
+ post.set('slug', 'test');
1990
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
1991
+ post.addAuthor({ name: 'Alice', slug: 'alice', email: 'alice@example.com' });
1992
+ post.addAuthor({ name: 'Bob', slug: 'bob', email: 'bob@example.com', roles: ['Editor'] });
1993
+ await post.save(ctx.db);
1994
+ const dir = join(tmpdir(), `mg-roles-json-${Date.now()}`);
1995
+ const files = await ctx.writeGhostJson(dir);
1996
+ const content = JSON.parse(await readFile(files[0].path, 'utf-8'));
1997
+ const alice = content.data.users.find((u) => u.slug === 'alice');
1998
+ const bob = content.data.users.find((u) => u.slug === 'bob');
1999
+ assert.deepEqual(alice.roles, ['Contributor']);
2000
+ assert.deepEqual(bob.roles, ['Editor']);
2001
+ assert.equal('role' in alice, false);
2002
+ assert.equal('role' in bob, false);
2003
+ await rm(dir, { recursive: true, force: true });
2004
+ await ctx.close();
2005
+ });
2006
+ it('Gives each author its own roles array', async () => {
2007
+ const first = new AuthorContext({ name: 'A', slug: 'a', email: 'a@example.com' });
2008
+ const second = new AuthorContext({ name: 'B', slug: 'b', email: 'b@example.com' });
2009
+ assert.deepEqual(first.data.roles, ['Contributor']);
2010
+ assert.notEqual(first.data.roles, second.data.roles);
2011
+ first.data.roles.push('Editor');
2012
+ assert.deepEqual(second.data.roles, ['Contributor']);
2013
+ });
2014
+ });
1945
2015
  describe('writeGhostUsersJson', () => {
1946
2016
  it('Writes all users to a single JSON file', async () => {
1947
2017
  const ctx = new MigrateContext();
@@ -1964,6 +2034,9 @@ describe('MigrateContext', () => {
1964
2034
  assert.ok(content.meta.exported_on);
1965
2035
  assert.ok(!content.data.posts);
1966
2036
  assert.ok(!content.data.posts_authors);
2037
+ // Ghost expects an array of role names, not a single "role" string
2038
+ assert.deepEqual(content.data.users[0].roles, ['Contributor']);
2039
+ assert.equal('role' in content.data.users[0], false);
1967
2040
  await rm(dir, { recursive: true, force: true });
1968
2041
  await ctx.close();
1969
2042
  });
@@ -2013,6 +2086,96 @@ describe('MigrateContext', () => {
2013
2086
  });
2014
2087
  });
2015
2088
  describe('deduplicateSlugs', () => {
2089
+ it('Stores the pre-deduplication slug in original_slug', async () => {
2090
+ const instance = new MigrateContext();
2091
+ await instance.init();
2092
+ const post1 = await instance.addPost();
2093
+ post1.set('title', 'Older Post');
2094
+ post1.set('slug', 'my-post');
2095
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2096
+ await post1.save(instance.db);
2097
+ const post2 = await instance.addPost();
2098
+ post2.set('title', 'Newer Post');
2099
+ post2.set('slug', 'my-post');
2100
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2101
+ await post2.save(instance.db);
2102
+ const rows = instance.db.db.prepare('SELECT slug, original_slug FROM Posts ORDER BY id ASC').all();
2103
+ assert.equal(rows.length, 2);
2104
+ assert.equal(rows[0].slug, 'my-post');
2105
+ assert.equal(rows[0].original_slug, 'my-post');
2106
+ assert.equal(rows[1].slug, 'my-post-2');
2107
+ assert.equal(rows[1].original_slug, 'my-post');
2108
+ await instance.close();
2109
+ });
2110
+ it('Leaves original_slug untouched when a post is updated', async () => {
2111
+ const instance = new MigrateContext();
2112
+ await instance.init();
2113
+ const post1 = await instance.addPost();
2114
+ post1.set('title', 'Older Post');
2115
+ post1.set('slug', 'my-post');
2116
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2117
+ await post1.save(instance.db);
2118
+ const post2 = await instance.addPost();
2119
+ post2.set('title', 'Newer Post');
2120
+ post2.set('slug', 'my-post');
2121
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2122
+ await post2.save(instance.db);
2123
+ post2.set('slug', 'renamed-by-hand');
2124
+ await post2.save(instance.db);
2125
+ const row = instance.db.db
2126
+ .prepare('SELECT slug, original_slug FROM Posts WHERE id = ?')
2127
+ .get(post2.dbId);
2128
+ assert.equal(row.slug, 'renamed-by-hand');
2129
+ assert.equal(row.original_slug, 'my-post');
2130
+ await instance.close();
2131
+ });
2132
+ it('Reports renames from an earlier run when resuming an existing dbPath', async () => {
2133
+ const filePath = join(tmpdir(), `mg-context-dedup-resume-${Date.now()}.sqlite`);
2134
+ const firstRun = new MigrateContext({ dbPath: filePath });
2135
+ await firstRun.init();
2136
+ const post1 = await firstRun.addPost({ source: { url: 'https://example.com/blog/my-post' } });
2137
+ post1.set('title', 'First');
2138
+ post1.set('slug', 'my-post');
2139
+ post1.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2140
+ await post1.save(firstRun.db);
2141
+ const post2 = await firstRun.addPost({ source: { url: 'https://example.com/news/my-post' } });
2142
+ post2.set('title', 'Second');
2143
+ post2.set('slug', 'my-post');
2144
+ post2.set('created_at', new Date('2023-06-01T00:00:00.000Z'));
2145
+ await post2.save(firstRun.db);
2146
+ await firstRun.close();
2147
+ // Resume against the same database — nothing is inserted this run, so the
2148
+ // in-memory renames are empty, but the group is still reported from the DB
2149
+ const secondRun = new MigrateContext({ dbPath: filePath });
2150
+ await secondRun.init();
2151
+ assert.equal(secondRun.db.slugRenames.length, 0);
2152
+ const duplicates = await secondRun.deduplicateSlugs();
2153
+ assert.equal(duplicates.length, 2);
2154
+ assert.equal(duplicates[0].oldSlug, 'my-post');
2155
+ assert.equal(duplicates[0].newSlug, 'my-post');
2156
+ assert.equal(duplicates[0].url, 'https://example.com/blog/my-post');
2157
+ assert.equal(duplicates[1].oldSlug, 'my-post');
2158
+ assert.equal(duplicates[1].newSlug, 'my-post-2');
2159
+ assert.equal(duplicates[1].url, 'https://example.com/news/my-post');
2160
+ await secondRun.close();
2161
+ await unlink(filePath);
2162
+ });
2163
+ it('Ignores a slug that was changed after insert', async () => {
2164
+ const instance = new MigrateContext();
2165
+ await instance.init();
2166
+ const post = await instance.addPost();
2167
+ post.set('title', 'Only Post');
2168
+ post.set('slug', 'original-slug');
2169
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2170
+ await post.save(instance.db);
2171
+ // Renaming after insert leaves original_slug behind, but no post holds
2172
+ // "original-slug" any more, so this was not a duplicate-slug collision
2173
+ post.set('slug', 'renamed-by-hand');
2174
+ await post.save(instance.db);
2175
+ const duplicates = await instance.deduplicateSlugs();
2176
+ assert.equal(duplicates.length, 0);
2177
+ await instance.close();
2178
+ });
2016
2179
  it('Returns empty array when no duplicates exist', async () => {
2017
2180
  const instance = new MigrateContext();
2018
2181
  await instance.init();
@@ -2292,5 +2455,62 @@ describe('MigrateContext', () => {
2292
2455
  await instance.close();
2293
2456
  });
2294
2457
  });
2458
+ describe('sanitizedFields', () => {
2459
+ it('Reports values trimmed on the way into the database', async () => {
2460
+ const instance = new MigrateContext();
2461
+ await instance.init();
2462
+ const post = await instance.addPost();
2463
+ post.set('title', 'a'.repeat(300));
2464
+ post.set('slug', 'long-title-post');
2465
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2466
+ post.addTag({ name: 'b'.repeat(200), slug: 'long-tag' });
2467
+ post.addAuthor({ name: 'Jane', slug: 'jane', email: 'hello@world.com123' });
2468
+ await post.save(instance.db);
2469
+ const byField = new Map(instance.sanitizedFields.map((f) => [`${f.context}.${f.field}`, f]));
2470
+ assert.equal(byField.get('PostContext.title').reason, 'truncated');
2471
+ assert.equal(byField.get('PostContext.title').newValue.length, 255);
2472
+ assert.equal(byField.get('PostContext.title').slug, 'long-title-post');
2473
+ assert.equal(byField.get('TagContext.name').reason, 'truncated');
2474
+ assert.equal(byField.get('TagContext.name').newValue.length, 191);
2475
+ assert.equal(byField.get('TagContext.name').slug, 'long-tag');
2476
+ assert.equal(byField.get('AuthorContext.email').reason, 'replaced');
2477
+ assert.equal(byField.get('AuthorContext.email').newValue, 'hello-world@example.com');
2478
+ // The truncated values are what actually landed in the database
2479
+ const stored = await instance.findPosts({ slug: 'long-title-post' });
2480
+ assert.equal(stored[0].data.title.length, 255);
2481
+ assert.equal(stored[0].data.tags[0].data.name.length, 191);
2482
+ assert.equal(stored[0].data.authors[0].data.email, 'hello-world@example.com');
2483
+ await instance.close();
2484
+ });
2485
+ it('Is empty when nothing needed changing', async () => {
2486
+ const instance = new MigrateContext();
2487
+ await instance.init();
2488
+ const post = await instance.addPost();
2489
+ post.set('title', 'Fine');
2490
+ post.set('slug', 'fine');
2491
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2492
+ await post.save(instance.db);
2493
+ assert.equal(instance.sanitizedFields.length, 0);
2494
+ await instance.close();
2495
+ });
2496
+ it('Keeps a deduplicated slug within the maximum length', async () => {
2497
+ const instance = new MigrateContext();
2498
+ await instance.init();
2499
+ // A slug already at the maximum length — the "-2" must not push it over
2500
+ const longSlug = 'a'.repeat(191);
2501
+ for (const title of ['First', 'Second']) {
2502
+ const post = await instance.addPost();
2503
+ post.set('title', title);
2504
+ post.set('slug', longSlug);
2505
+ post.set('created_at', new Date('2023-01-01T00:00:00.000Z'));
2506
+ await post.save(instance.db);
2507
+ }
2508
+ const rows = instance.db.db.prepare('SELECT slug FROM Posts ORDER BY id ASC').all();
2509
+ assert.equal(rows[0].slug, longSlug);
2510
+ assert.equal(rows[1].slug, `${'a'.repeat(189)}-2`);
2511
+ assert.equal(rows[1].slug.length, 191);
2512
+ await instance.close();
2513
+ });
2514
+ });
2295
2515
  });
2296
2516
  //# sourceMappingURL=migrate-context.test.js.map