check-work-cover-extraction.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import assert from 'node:assert/strict';
  2. import { existsSync, readFileSync } from 'node:fs';
  3. import { Buffer } from 'node:buffer';
  4. import ts from 'typescript';
  5. const moduleUrl = new URL('../server/src/utils/platformWorkCover.ts', import.meta.url);
  6. assert.ok(
  7. existsSync(moduleUrl),
  8. 'server/src/utils/platformWorkCover.ts should export platform work cover helpers',
  9. );
  10. const source = readFileSync(moduleUrl, 'utf8');
  11. const { outputText } = ts.transpileModule(source, {
  12. compilerOptions: {
  13. module: ts.ModuleKind.ES2022,
  14. target: ts.ScriptTarget.ES2022,
  15. },
  16. });
  17. const dataUrl = `data:text/javascript;base64,${Buffer.from(outputText, 'utf8').toString('base64')}`;
  18. const { extractPlatformWorkCoverUrl, normalizePlatformImageUrl } = await import(dataUrl);
  19. assert.equal(normalizePlatformImageUrl('//example.com/a.jpg'), 'https://example.com/a.jpg');
  20. assert.equal(normalizePlatformImageUrl(' http://example.com/a.jpg?x=1 '), 'http://example.com/a.jpg?x=1');
  21. assert.equal(normalizePlatformImageUrl('not-a-url'), '');
  22. assert.equal(
  23. extractPlatformWorkCoverUrl({
  24. cover_images: '[{"src":"//bjh.example.com/cover-a.jpg"},{"src":"//bjh.example.com/cover-b.jpg"}]',
  25. }),
  26. 'https://bjh.example.com/cover-a.jpg',
  27. );
  28. assert.equal(
  29. extractPlatformWorkCoverUrl({
  30. coverImage: { imageUrl: 'https://bjh.example.com/cover-c.jpg' },
  31. }),
  32. 'https://bjh.example.com/cover-c.jpg',
  33. );
  34. assert.equal(
  35. extractPlatformWorkCoverUrl({
  36. objectDesc: {
  37. mediaList: [
  38. {
  39. coverUrl: 'https://wx.example.com/post-cover.jpg',
  40. },
  41. ],
  42. },
  43. }),
  44. 'https://wx.example.com/post-cover.jpg',
  45. );
  46. assert.equal(
  47. extractPlatformWorkCoverUrl({
  48. mediaSpec: {
  49. coverUrl: '//wx.example.com/media-cover.jpg',
  50. },
  51. }),
  52. 'https://wx.example.com/media-cover.jpg',
  53. );
  54. assert.equal(
  55. extractPlatformWorkCoverUrl({
  56. desc: {
  57. media: [
  58. {
  59. thumbUrl: 'https://wx.example.com/desc-media-thumb.jpg',
  60. coverUrl: 'https://wx.example.com/desc-media-cover.jpg',
  61. },
  62. ],
  63. },
  64. }),
  65. 'https://wx.example.com/desc-media-cover.jpg',
  66. );
  67. assert.equal(
  68. extractPlatformWorkCoverUrl({
  69. title: 'no image here',
  70. author: { avatarUrl: 'https://example.com/avatar.jpg' },
  71. }),
  72. '',
  73. );
  74. console.log('platform work cover extraction checks passed');