storeMonthlyPendingCount.test.mjs 5.17 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 { STORE_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
const adapters = require('../api/storeDailyReportAdapters.js')
const {
	normalizeMonthlyPendingCountResponse,
	extractPendingCountFromResponseData
} = adapters
const {
	STORE_MONTHLY_PENDING_FIELD_KEY,
	isMonthlyPendingFieldVisible,
	isMonthlyPendingValueEmpty,
	shouldFetchMonthlyPendingCount
} = require('../utils/store-monthly-pending.js')

const fieldF224 = { key: STORE_MONTHLY_PENDING_FIELD_KEY, type: 'numberInput' }

describe('store-monthly-pending utils', () => {
	it('isMonthlyPendingFieldVisible detects F2-24 in fixedFields', () => {
		assert.equal(
			isMonthlyPendingFieldVisible({ fixedFields: [fieldF224], sectionGroups: [] }),
			true
		)
		assert.equal(isMonthlyPendingFieldVisible({ fixedFields: [], sectionGroups: [] }), false)
	})

	it('isMonthlyPendingFieldVisible detects F2-24 in section group children', () => {
		assert.equal(
			isMonthlyPendingFieldVisible({
				fixedFields: [],
				sectionGroups: [{ type: 'group', children: [fieldF224] }]
			}),
			true
		)
	})

	it('isMonthlyPendingValueEmpty treats 0 as filled', () => {
		assert.equal(isMonthlyPendingValueEmpty({}), true)
		assert.equal(isMonthlyPendingValueEmpty({ 'F2-24': '' }), true)
		assert.equal(isMonthlyPendingValueEmpty({ 'F2-24': null }), true)
		assert.equal(isMonthlyPendingValueEmpty({ 'F2-24': 0 }), false)
		assert.equal(isMonthlyPendingValueEmpty({ 'F2-24': 5 }), false)
	})

	it('shouldFetchMonthlyPendingCount requires today, visible, and empty', () => {
		const viewState = { fixedFields: [fieldF224], sectionGroups: [] }
		assert.equal(
			shouldFetchMonthlyPendingCount({
				isTodayReport: true,
				viewState,
				values: {}
			}),
			true
		)
		assert.equal(
			shouldFetchMonthlyPendingCount({
				isTodayReport: false,
				viewState,
				values: {}
			}),
			false
		)
		assert.equal(
			shouldFetchMonthlyPendingCount({
				isTodayReport: true,
				viewState: { fixedFields: [], sectionGroups: [] },
				values: {}
			}),
			false
		)
		assert.equal(
			shouldFetchMonthlyPendingCount({
				isTodayReport: true,
				viewState,
				values: { 'F2-24': 3 }
			}),
			false
		)
	})
})

describe('fetchMonthlyPendingCount', () => {
	it('IT-STORE-PENDING-ADAPTER-001: mock adapter returns fixed pendingCount', async () => {
		const original = STORE_DAILY_API_SWITCH.monthlyPendingCount
		STORE_DAILY_API_SWITCH.monthlyPendingCount = 'mock'
		try {
			const res = await adapters.fetchMonthlyPendingCountAdapter({
				merchantId: '4s-1',
				reportDate: '2026-06-03'
			})
			assert.equal(res.code, 0)
			assert.equal(res.data.pendingCount, 12)
		} finally {
			STORE_DAILY_API_SWITCH.monthlyPendingCount = original
		}
	})

	it('IT-STORE-PENDING-ADAPTER-002: real adapter calls monthly.pending.count POST', async () => {
		const original = STORE_DAILY_API_SWITCH.monthlyPendingCount
		STORE_DAILY_API_SWITCH.monthlyPendingCount = 'real'
		let captured = null
		adapters.__setDynamicDailyReportApi({
			dailyReportMonthlyPendingCountGet: async data => {
				captured = data
				return { code: 0, message: 'ok', data: { pendingCount: 7 } }
			}
		})
		try {
			const res = await adapters.fetchMonthlyPendingCountAdapter({
				merchantId: '4s-2',
				reportDate: '2026-06-03',
				sceneType: 1
			})
			assert.equal(captured.merchantId, '4s-2')
			assert.equal(captured.reportDate, '2026-06-03')
			assert.equal(captured.sceneType, 1)
			assert.equal(res.data.pendingCount, 7)
		} finally {
			adapters.__setDynamicDailyReportApi(null)
			STORE_DAILY_API_SWITCH.monthlyPendingCount = original
		}
	})

	it('IT-STORE-PENDING-FACADE-001: normalizeMonthlyPendingCountResponse parses pendingCount', () => {
		const ok = normalizeMonthlyPendingCountResponse({
			code: 0,
			message: 'ok',
			data: { pendingCount: 8 }
		})
		assert.equal(ok.code, 0)
		assert.equal(ok.data.pendingCount, 8)

		const zero = normalizeMonthlyPendingCountResponse({
			code: 0,
			data: { pendingCount: 0 }
		})
		assert.equal(zero.data.pendingCount, 0)
	})

	it('IT-STORE-PENDING-FACADE-002: invalid pendingCount in response fails', () => {
		const bad = normalizeMonthlyPendingCountResponse({
			code: 0,
			data: { pendingCount: 'n/a' }
		})
		assert.notEqual(bad.code, 0)
		assert.equal(bad.data, null)
	})

	it('IT-STORE-PENDING-FACADE-003: normalize reads pendingcount lowercase from API', () => {
		const res = normalizeMonthlyPendingCountResponse({
			code: 0,
			message: 'ok',
			data: { pendingcount: 15 }
		})
		assert.equal(res.code, 0)
		assert.equal(res.data.pendingCount, 15)
	})

	it('IT-STORE-PENDING-FACADE-004: normalize reads scalar data value', () => {
		const res = normalizeMonthlyPendingCountResponse({
			code: 0,
			data: 9
		})
		assert.equal(res.data.pendingCount, 9)
	})

	it('IT-STORE-PENDING-EXTRACT-001: extractPendingCountFromResponseData priority', () => {
		assert.equal(extractPendingCountFromResponseData({ pendingcount: 3 }), 3)
		assert.equal(extractPendingCountFromResponseData({ pendingCount: 4 }), 4)
		assert.equal(extractPendingCountFromResponseData(11), 11)
		assert.equal(extractPendingCountFromResponseData('6'), 6)
	})
})