import assert from 'node:assert/strict';
import { extractDeclaredNotesCountFromPostedResponse, extractNotesCountFromPostedResponse } from '../utils/xiaohongshu.js';

const sample = {
  success: true,
  data: {
    notes: new Array(12).fill(null).map((_, i) => ({ id: String(i + 1) })),
    tags: [
      { id: 'special.note_time_desc', notes_count: 368 },
      { id: 'other', notes_count: 12 },
    ],
  },
};

assert.equal(extractNotesCountFromPostedResponse(sample), 368);
assert.equal(extractDeclaredNotesCountFromPostedResponse(sample), 368);

const sampleMaxTag = {
  data: {
    notes: [],
    tags: [
      { id: 'a', notes_count: 10 },
      { id: 'b', notes_count: 20 },
    ],
  },
};
assert.equal(extractNotesCountFromPostedResponse(sampleMaxTag), 20);
assert.equal(extractDeclaredNotesCountFromPostedResponse(sampleMaxTag), 20);

const sampleTotalOnly = {
  data: {
    notes: [{ id: '1' }],
    total_count: 99,
  },
};
assert.equal(extractNotesCountFromPostedResponse(sampleTotalOnly), 99);
assert.equal(extractDeclaredNotesCountFromPostedResponse(sampleTotalOnly), 99);

const sampleFallback = {
  data: {
    notes: [{ id: '1' }, { id: '2' }],
  },
};
assert.equal(extractNotesCountFromPostedResponse(sampleFallback), 2);
assert.equal(extractDeclaredNotesCountFromPostedResponse(sampleFallback), 0);

console.log('selftest-xhs passed');

