diff --git a/packages/examination-utils/__tests__/examination-period.test.ts b/packages/examination-utils/__tests__/examination-period.test.ts index 955a088..493c7c0 100644 --- a/packages/examination-utils/__tests__/examination-period.test.ts +++ b/packages/examination-utils/__tests__/examination-period.test.ts @@ -1,4 +1,4 @@ -import { test, describe } from 'node:test' +import { test, describe, beforeEach } from 'node:test' import assert from 'node:assert/strict' describe('examination-period', () => { @@ -25,5 +25,49 @@ describe('examination-period', () => { const currentExaminationPeriod = getCurrentExaminationPeriod() assert.equal(currentExaminationPeriod, '2012S') }) + + describe('examination period override', () => { + beforeEach(() => { + delete process.env.EXAMINATION_PERIOD_OVERRIDE + }) + + test('should use the clock when EXAMINATION_PERIOD_OVERRIDE is undefined', async context => { + context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) + + const { getCurrentExaminationPeriod } = await import('../src/examination-period.js') + assert.equal(getCurrentExaminationPeriod(), '2012K') + }) + + test('should use EXAMINATION_PERIOD_OVERRIDE when it is set', async context => { + context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) + process.env.EXAMINATION_PERIOD_OVERRIDE = '2027K' + + const { getCurrentExaminationPeriod } = await import('../src/examination-period.js') + assert.equal(getCurrentExaminationPeriod(), '2027K') + }) + + test('should use the clock when EXAMINATION_PERIOD_OVERRIDE is empty', async context => { + context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) + process.env.EXAMINATION_PERIOD_OVERRIDE = '' + + const { getCurrentExaminationPeriod } = await import('../src/examination-period.js') + assert.equal(getCurrentExaminationPeriod(), '2012K') + }) + + test('should use the clock when process does not exist, because in frontend the browser does not have node.js', async context => { + context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) + const { getCurrentExaminationPeriod } = await import('../src/examination-period.js') + + const processDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'process')! + Object.defineProperty(globalThis, 'process', { value: undefined, configurable: true }) + let examinationPeriod: string + try { + examinationPeriod = getCurrentExaminationPeriod() + } finally { + Object.defineProperty(globalThis, 'process', processDescriptor) + } + assert.equal(examinationPeriod, '2012K') + }) + }) }) }) diff --git a/packages/examination-utils/src/examination-period.ts b/packages/examination-utils/src/examination-period.ts index 25df380..9c82b7f 100644 --- a/packages/examination-utils/src/examination-period.ts +++ b/packages/examination-utils/src/examination-period.ts @@ -1,4 +1,7 @@ export function getCurrentExaminationPeriod(): string { + const override = globalThis.process?.env?.EXAMINATION_PERIOD_OVERRIDE + if (override) return override + const date = new Date() const year = date.getFullYear() const month = date.getMonth() + 1