Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion packages/examination-utils/__tests__/examination-period.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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')
})
})
})
})
3 changes: 3 additions & 0 deletions packages/examination-utils/src/examination-period.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading