snapshot.service.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import assert from 'node:assert/strict';
  2. import test from 'node:test';
  3. import type { Queryable } from '../src/db/types.js';
  4. import { SnapshotService } from '../src/modules/domestic-voc/services/snapshot.service.js';
  5. test('snapshot returns the frontend DomesticDataset contract for an empty workspace', async () => {
  6. const database = {
  7. async query(text: string) {
  8. if (text.includes('FROM voc.workspace')) {
  9. return { rows: [{ id: '1', public_id: 'demashi', case_name: 'Demashi' }], rowCount: 1 };
  10. }
  11. return { rows: [], rowCount: 0 };
  12. },
  13. } as unknown as Queryable;
  14. const service = new SnapshotService(database, () => new Date('2026-07-23T00:00:00.000Z'));
  15. const snapshot = await service.getSnapshot('demashi', 'jd');
  16. assert.ok(snapshot);
  17. assert.equal(snapshot.schemaVersion, 1);
  18. assert.equal(snapshot.caseName, 'Demashi');
  19. assert.equal(snapshot.platform, 'jd');
  20. assert.deepEqual(snapshot.products, []);
  21. assert.deepEqual(snapshot.reviews, []);
  22. assert.deepEqual(snapshot.dailyTotals, []);
  23. assert.deepEqual(snapshot.source.dateRange, { start: '', end: '' });
  24. assert.equal(snapshot.summary.reviewCount, 0);
  25. });
  26. test('snapshot exposes relation groups without leaking relation stub products', async () => {
  27. const database = {
  28. async query(text: string) {
  29. if (text.includes('FROM voc.workspace')) {
  30. return { rows: [{ id: '1', public_id: 'demashi', case_name: 'Demashi' }], rowCount: 1 };
  31. }
  32. if (text.includes('FROM voc.product_relation')) {
  33. return {
  34. rows: [{
  35. relation_key: 'jd:own-1:competitor-1',
  36. own_internal_id: '10',
  37. own_product_id: 'own-1',
  38. own_product_key: 'jd:own-1',
  39. competitor_internal_id: '11',
  40. competitor_product_id: 'competitor-1',
  41. competitor_product_key: 'jd:competitor-1',
  42. competitor_brand: 'Competitor',
  43. own_model: 'DM-1',
  44. own_category_1: '',
  45. own_category_2: '',
  46. own_category_3: 'Kitchen',
  47. category: 'Kitchen',
  48. }],
  49. rowCount: 1,
  50. };
  51. }
  52. return { rows: [], rowCount: 0 };
  53. },
  54. } as unknown as Queryable;
  55. const service = new SnapshotService(database);
  56. const snapshot = await service.getSnapshot('demashi', 'jd');
  57. assert.ok(snapshot);
  58. assert.deepEqual(snapshot.products, []);
  59. assert.equal(snapshot.relations.length, 1);
  60. assert.equal(snapshot.mappingGroups.length, 1);
  61. assert.equal(snapshot.mappingGroups[0]?.model, 'DM-1');
  62. assert.equal(snapshot.summary.uniqueCompetitorProducts, 1);
  63. });