dailyReportWeeklyList.test.mjs 4.82 KB
import { describe, it, beforeEach } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const { DAILY_REPORT_TEMPLATE_TYPE } = require('../config/dailyReportConstants.js')
const staffAdapters = require('../api/staffDailyReportAdapters.js')
const storeAdapters = require('../api/storeDailyReportAdapters.js')
const negotiationAdapters = require('../api/negotiationDailyReportAdapters.js')
const {
	STAFF_DAILY_API_SWITCH,
	STORE_DAILY_API_SWITCH,
	NEGOTIATION_DAILY_API_SWITCH
} = require('../config/apiSwitch.js')

describe('dailyReportWeeklyList adapters', () => {
	beforeEach(() => {
		staffAdapters.__setDynamicDailyReportApi(null)
		storeAdapters.__setDynamicDailyReportApi(null)
		negotiationAdapters.__setDynamicDailyReportApi(null)
	})

	it('fetchWeeklyAdapter staff real mode calls dailyReportWeeklyList with templateType=1', async () => {
		const original = STAFF_DAILY_API_SWITCH.weekly
		STAFF_DAILY_API_SWITCH.weekly = 'real'
		let captured = null
		staffAdapters.__setDynamicDailyReportApi({
			dailyReportWeeklyList: async data => {
				captured = data
				return { code: 0, message: 'ok', data: ['2026-05-26'] }
			}
		})
		try {
			const res = await staffAdapters.fetchWeeklyAdapter({
				merchantId: 1001,
				staffUuid: 'uuid-1',
				sceneType: 1,
				reportStartDate: '2026-05-20',
				reportEndDate: '2026-05-26'
			})
			assert.equal(captured.merchantId, 1001)
			assert.equal(captured.staffUuid, 'uuid-1')
			assert.equal(captured.sceneType, 1)
			assert.equal(captured.templateType, DAILY_REPORT_TEMPLATE_TYPE.staff)
			assert.equal(captured.reportStartDate, '2026-05-20')
			assert.equal(captured.reportEndDate, '2026-05-26')
			assert.deepEqual(res.data, ['2026-05-26'])
		} finally {
			staffAdapters.__setDynamicDailyReportApi(null)
			STAFF_DAILY_API_SWITCH.weekly = original
		}
	})

	it('fetchWeeklyAdapter store real mode uses templateType=2', async () => {
		const original = STORE_DAILY_API_SWITCH.weekly
		STORE_DAILY_API_SWITCH.weekly = 'real'
		let captured = null
		storeAdapters.__setDynamicDailyReportApi({
			dailyReportWeeklyList: async data => {
				captured = data
				return { code: 0, message: 'ok', data: ['2026-05-25'] }
			}
		})
		try {
			const res = await storeAdapters.fetchWeeklyAdapter({
				merchantId: 2002,
				staffUuid: 'uuid-2',
				reportStartDate: '2026-05-20',
				reportEndDate: '2026-05-26',
				templateType: DAILY_REPORT_TEMPLATE_TYPE.store
			})
			assert.equal(captured.templateType, 2)
			assert.deepEqual(res.data, ['2026-05-25'])
		} finally {
			storeAdapters.__setDynamicDailyReportApi(null)
			STORE_DAILY_API_SWITCH.weekly = original
		}
	})

	it('fetchWeeklyAdapter negotiation real mode uses templateType=3', async () => {
		const original = NEGOTIATION_DAILY_API_SWITCH.weekly
		NEGOTIATION_DAILY_API_SWITCH.weekly = 'real'
		let captured = null
		negotiationAdapters.__setDynamicDailyReportApi({
			dailyReportWeeklyList: async data => {
				captured = data
				return { code: 0, message: 'ok', data: ['2026-05-24'] }
			}
		})
		try {
			const res = await negotiationAdapters.fetchWeeklyAdapter({
				merchantId: 3003,
				staffUuid: 'uuid-3',
				reportStartDate: '2026-05-20',
				reportEndDate: '2026-05-26'
			})
			assert.equal(captured.merchantId, 3003)
			assert.equal(captured.staffUuid, 'uuid-3')
			assert.equal(captured.templateType, DAILY_REPORT_TEMPLATE_TYPE.negotiation)
			assert.equal(captured.templateType, 3)
			assert.deepEqual(res.data, ['2026-05-24'])
		} finally {
			negotiationAdapters.__setDynamicDailyReportApi(null)
			NEGOTIATION_DAILY_API_SWITCH.weekly = original
		}
	})

	it('fetchWeeklyAdapter mock mode returns empty list', async () => {
		const original = STAFF_DAILY_API_SWITCH.weekly
		STAFF_DAILY_API_SWITCH.weekly = 'mock'
		let called = false
		staffAdapters.__setDynamicDailyReportApi({
			dailyReportWeeklyList: async () => {
				called = true
				return { code: 0, data: ['2026-05-26'] }
			}
		})
		try {
			const res = await staffAdapters.fetchWeeklyAdapter({ merchantId: 1 })
			assert.equal(called, false)
			assert.deepEqual(res.data, [])
		} finally {
			staffAdapters.__setDynamicDailyReportApi(null)
			STAFF_DAILY_API_SWITCH.weekly = original
		}
	})

	it('fetchWeeklyAdapter negotiation mock mode returns empty list', async () => {
		const original = NEGOTIATION_DAILY_API_SWITCH.weekly
		NEGOTIATION_DAILY_API_SWITCH.weekly = 'mock'
		let called = false
		negotiationAdapters.__setDynamicDailyReportApi({
			dailyReportWeeklyList: async () => {
				called = true
				return { code: 0, data: ['2026-05-26'] }
			}
		})
		try {
			const res = await negotiationAdapters.fetchWeeklyAdapter({ merchantId: 1 })
			assert.equal(called, false)
			assert.deepEqual(res.data, [])
		} finally {
			negotiationAdapters.__setDynamicDailyReportApi(null)
			NEGOTIATION_DAILY_API_SWITCH.weekly = original
		}
	})
})