plan-import-date-value.test.mjs 2.15 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 {
	PLAN_IMPORT_DATE_FIELD_KEY,
	wrapPlanImportDate,
	unwrapPlanImportDate,
	hasPlanImportDateMarker,
	clearPlanImportDateMarker,
	maybeRestorePlanImportDateMarker
} = require('../form-engine/plan-import-date-value.js')

function tomorrowYmd() {
	const d = new Date()
	d.setDate(d.getDate() + 1)
	const y = d.getFullYear()
	const m = `${d.getMonth() + 1}`.padStart(2, '0')
	const day = `${d.getDate()}`.padStart(2, '0')
	return `${y}-${m}-${day}`
}

describe('plan-import-date-value', () => {
	it('IT-PLAN-DATE-001: wrap and unwrap round-trip', () => {
		const wrapped = wrapPlanImportDate('2026-12-31')
		assert.equal(wrapped.value, '2026-12-31')
		assert.equal(wrapped.partialFromPlanImport, true)
		assert.equal(unwrapPlanImportDate(wrapped), '2026-12-31')
		assert.equal(hasPlanImportDateMarker(wrapped), true)
	})

	it('IT-PLAN-DATE-002: clearPlanImportDateMarker returns plain string', () => {
		const wrapped = wrapPlanImportDate('2026-12-31')
		assert.equal(clearPlanImportDateMarker(wrapped), '2026-12-31')
		assert.equal(hasPlanImportDateMarker(clearPlanImportDateMarker(wrapped)), false)
	})

	it('IT-PLAN-DATE-003: maybeRestore only for F3-4-1 future dates', () => {
		const future = tomorrowYmd()
		const restored = maybeRestorePlanImportDateMarker(PLAN_IMPORT_DATE_FIELD_KEY, future)
		assert.equal(hasPlanImportDateMarker(restored), true)
		assert.equal(unwrapPlanImportDate(restored), future)
		assert.equal(maybeRestorePlanImportDateMarker('F3-2-1', future), future)
	})

	it('IT-PLAN-DATE-004: maybeRestore skips past dates', () => {
		const past = '2000-01-01'
		assert.equal(maybeRestorePlanImportDateMarker(PLAN_IMPORT_DATE_FIELD_KEY, past), past)
	})

	it('IT-PLAN-DATE-005: wrap uses literal partialFromPlanImport key', () => {
		const wrapped = wrapPlanImportDate('2026-12-31')
		assert.ok(Object.prototype.hasOwnProperty.call(wrapped, 'partialFromPlanImport'))
		assert.equal(wrapped.partialFromPlanImport, true)
		assert.equal(Object.keys(wrapped).filter(k => k === 'undefined').length, 0)
	})
})