storeMonthlyPendingCount.test.mjs 6.92 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,
	STORE_MONTHLY_PENDING_FIELD_KEYS,
	isMonthlyPendingFieldVisible,
	isMonthlyPendingValueEmpty,
	shouldFetchMonthlyPendingCount,
	resolveMonthlyPendingFieldKey,
	resolveMonthlyPendingInitialization
} = require('../utils/store-monthly-pending.js')

const fieldF224 = { key: STORE_MONTHLY_PENDING_FIELD_KEY, type: 'numberInput' }
const fieldF2102 = { key: 'F2-102', 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
		)
	})

	it('IT-STORE-PENDING-V230-001: resolves F2-102 first and keeps F2-24 compatibility', () => {
		assert.deepEqual(STORE_MONTHLY_PENDING_FIELD_KEYS, ['F2-102', 'F2-24'])
		assert.equal(
			resolveMonthlyPendingFieldKey({
				fixedFields: [fieldF224, fieldF2102],
				sectionGroups: []
			}),
			'F2-102'
		)
		assert.equal(
			resolveMonthlyPendingFieldKey({
				fixedFields: [fieldF224],
				sectionGroups: []
			}),
			'F2-24'
		)
	})

	it('IT-STORE-PENDING-V230-002: first day initializes F2-102 to zero locally', () => {
		assert.deepEqual(
			resolveMonthlyPendingInitialization({
				isTodayReport: true,
				reportDate: '2026-08-01',
				viewState: { fixedFields: [fieldF2102], sectionGroups: [] },
				values: {}
			}),
			{ type: 'set', fieldKey: 'F2-102', value: 0 }
		)
	})

	it('IT-STORE-PENDING-V230-003: non-first day fetches latest value for F2-102', () => {
		assert.deepEqual(
			resolveMonthlyPendingInitialization({
				isTodayReport: true,
				reportDate: '2026-08-14',
				viewState: { fixedFields: [fieldF2102], sectionGroups: [] },
				values: {}
			}),
			{ type: 'fetch', fieldKey: 'F2-102' }
		)
	})

	it('IT-STORE-PENDING-V230-004: existing value and historical report do not initialize', () => {
		const viewState = { fixedFields: [fieldF2102], sectionGroups: [] }
		assert.equal(
			resolveMonthlyPendingInitialization({
				isTodayReport: true,
				reportDate: '2026-08-14',
				viewState,
				values: { 'F2-102': 3 }
			}),
			null
		)
		assert.equal(
			resolveMonthlyPendingInitialization({
				isTodayReport: false,
				reportDate: '2026-08-14',
				viewState,
				values: {}
			}),
			null
		)
	})
})

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)
	})
})