negotiationTodayPlanStoreAdapters.test.mjs 2.34 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const timeUtil = require('../../utils/timeFormat/time-utils.js')
const { NEGOTIATION_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
const {
	buildMockTodayPlanStoreList,
	fetchTodayPlanStoreCountAdapter,
	fetchTodayPlanStoreListAdapter,
	normalizeTodayPlanStoreCountResponse,
	normalizeTodayPlanStoreListResponse,
	extractTodayPlanCount
} = require('../api/negotiationTodayPlanStoreAdapters.js')

describe('negotiationTodayPlanStoreAdapters', () => {
	it('IT-PLAN-ADAPTER-001: mock count matches today list length', async () => {
		const original = NEGOTIATION_DAILY_API_SWITCH.todayPlanStore
		NEGOTIATION_DAILY_API_SWITCH.todayPlanStore = 'mock'
		try {
			const today = timeUtil.getTodayDateStr()
			const expectedLen = buildMockTodayPlanStoreList(today).length
			const res = await fetchTodayPlanStoreCountAdapter({
				merchantId: '1',
				advanceUserId: 'u1',
				inviteDate: today
			})
			const normalized = normalizeTodayPlanStoreCountResponse(res)
			assert.equal(normalized.code, 0)
			assert.equal(normalized.data, expectedLen)
			assert.ok(expectedLen > 0)
		} finally {
			NEGOTIATION_DAILY_API_SWITCH.todayPlanStore = original
		}
	})

	it('IT-PLAN-ADAPTER-002: mock list empty for non-today inviteDate', async () => {
		const original = NEGOTIATION_DAILY_API_SWITCH.todayPlanStore
		NEGOTIATION_DAILY_API_SWITCH.todayPlanStore = 'mock'
		try {
			const res = await fetchTodayPlanStoreListAdapter({
				merchantId: '1',
				advanceUserId: 'u1',
				inviteDate: '2000-01-01'
			})
			const normalized = normalizeTodayPlanStoreListResponse(res)
			assert.equal(normalized.code, 0)
			assert.equal(normalized.data.length, 0)
		} finally {
			NEGOTIATION_DAILY_API_SWITCH.todayPlanStore = original
		}
	})

	it('IT-PLAN-ADAPTER-003: normalize count from scalar data', () => {
		assert.equal(extractTodayPlanCount(3), 3)
		const ok = normalizeTodayPlanStoreCountResponse({ code: 0, message: 'ok', data: 8 })
		assert.equal(ok.data, 8)
	})

	it('IT-PLAN-ADAPTER-004: normalize list response', () => {
		const res = normalizeTodayPlanStoreListResponse({
			code: 0,
			message: 'ok',
			data: [{ customerName: 'A' }]
		})
		assert.equal(res.data.length, 1)
		assert.equal(res.data[0].customerName, 'A')
	})
})