Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ishiko732 committed Nov 24, 2024
1 parent 5bdd308 commit 6098b02
Showing 1 changed file with 185 additions and 0 deletions.
185 changes: 185 additions & 0 deletions __tests__/strategies/seed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import {
AbstractScheduler,
Card,
createEmptyCard,
DefaultInitSeedStrategy,
fsrs,
GenSeedStrategyWithCardId,
Rating,
StrategyMode,
} from '../../src/fsrs'

interface ICard extends Card {
card_id: number
}

describe('seed strategy', () => {
it('default seed strategy', () => {
const seedStrategy = DefaultInitSeedStrategy
const f = fsrs().useStrategy(StrategyMode.SEED, seedStrategy)
const now = Date.UTC(2022, 11, 29, 12, 30, 0, 0)

const card = createEmptyCard<ICard>(now, (card: Card) => {
Object.assign(card, { card_id: 555 })
return card as ICard
})

const record = f.repeat(card, now)
const scheduler = new f['Scheduler'](card, now, f, {
seed: seedStrategy,
}) as AbstractScheduler

const seed = seedStrategy.bind(scheduler)()
console.debug('seed', seed)

expect(f['_seed']).toBe(seed)
})
})

describe('seed strategy with card ID', () => {
it('use seedStrategy', () => {
const seedStrategy = GenSeedStrategyWithCardId('card_id')
const f = fsrs().useStrategy(StrategyMode.SEED, seedStrategy)

expect(f['strategyHandler'].get(StrategyMode.SEED)).toBe(seedStrategy)

f.clearStrategy()
expect(f['strategyHandler'].get(StrategyMode.SEED)).toBeUndefined()
})
it('clear seedStrategy', () => {
const seedStrategy = GenSeedStrategyWithCardId('card_id')
const f = fsrs().useStrategy(StrategyMode.SEED, seedStrategy)
const now = Date.UTC(2022, 11, 29, 12, 30, 0, 0)

const card = createEmptyCard<ICard>(now, (card: Card) => {
Object.assign(card, { card_id: 555 })
return card as ICard
})

f.repeat(card, now)
let scheduler = new f['Scheduler'](card, now, f, {
seed: seedStrategy,
}) as AbstractScheduler

const seed_with_card_id = seedStrategy.bind(scheduler)()
console.debug('seed with card_id=555', seed_with_card_id)

f.clearStrategy(StrategyMode.SEED)

f.repeat(card, now)
scheduler = new f['Scheduler'](card, now, f, {
seed: DefaultInitSeedStrategy,
}) as AbstractScheduler
const basic_seed = DefaultInitSeedStrategy.bind(scheduler)()
console.debug('basic_seed with card_id=555', basic_seed)

expect(f['_seed']).toBe(basic_seed)

expect(seed_with_card_id).not.toBe(basic_seed)
})

it('exist card_id', () => {
const seedStrategy = GenSeedStrategyWithCardId('card_id')
const f = fsrs().useStrategy(StrategyMode.SEED, seedStrategy)
const now = Date.UTC(2022, 11, 29, 12, 30, 0, 0)

const card = createEmptyCard<ICard>(now, (card: Card) => {
Object.assign(card, { card_id: 555 })
return card as ICard
})

const record = f.repeat(card, now)
const scheduler = new f['Scheduler'](card, now, f, {
seed: seedStrategy,
}) as AbstractScheduler

const seed = seedStrategy.bind(scheduler)()
console.debug('seed with card_id=555', seed)

expect(f['_seed']).toBe(seed)
})

it('not exist card_id', () => {
const seedStrategy = GenSeedStrategyWithCardId('card_id')
const f = fsrs().useStrategy(StrategyMode.SEED, seedStrategy)
const now = Date.UTC(2022, 11, 29, 12, 30, 0, 0)

const card = createEmptyCard<ICard>(now)

const record = f.repeat(card, now)
const scheduler = new f['Scheduler'](card, now, f, {
seed: seedStrategy,
}) as AbstractScheduler

const seed = seedStrategy.bind(scheduler)()
console.debug('seed with card_id=undefined(default)', seed)

expect(f['_seed']).toBe(seed)
})

it('card_id = -1', () => {
const seedStrategy = GenSeedStrategyWithCardId('card_id')
const f = fsrs().useStrategy(StrategyMode.SEED, seedStrategy)
const now = Date.UTC(2022, 11, 29, 12, 30, 0, 0)

const card = createEmptyCard<ICard>(now, (card: Card) => {
Object.assign(card, { card_id: -1 })
return card as ICard
})

const record = f.repeat(card, now)
const scheduler = new f['Scheduler'](card, now, f, {
seed: seedStrategy,
}) as AbstractScheduler

const seed = seedStrategy.bind(scheduler)()
console.debug('with card_id=-1', seed)

expect(f['_seed']).toBe(seed)
expect(f['_seed']).toBe('0')
})

it('card_id is undefined', () => {
const seedStrategy = GenSeedStrategyWithCardId('card_id')
const f = fsrs().useStrategy(StrategyMode.SEED, seedStrategy)
const now = Date.UTC(2022, 11, 29, 12, 30, 0, 0)

const card = createEmptyCard<ICard>(now, (card: Card) => {
Object.assign(card, { card_id: undefined })
return card as ICard
})

const item = f.next(card, now, Rating.Good)
const scheduler = new f['Scheduler'](card, now, f, {
seed: seedStrategy,
}) as AbstractScheduler

const seed = seedStrategy.bind(scheduler)()
console.debug('seed with card_id=undefined', seed)

expect(f['_seed']).toBe(seed)
expect(f['_seed']).toBe(`${item.card.reps}`)
})

it('card_id is null', () => {
const seedStrategy = GenSeedStrategyWithCardId('card_id')
const f = fsrs().useStrategy(StrategyMode.SEED, seedStrategy)
const now = Date.UTC(2022, 11, 29, 12, 30, 0, 0)

const card = createEmptyCard<ICard>(now, (card: Card) => {
Object.assign(card, { card_id: null })
return card as ICard
})

const item = f.next(card, now, Rating.Good)
const scheduler = new f['Scheduler'](card, now, f, {
seed: seedStrategy,
}) as AbstractScheduler

const seed = seedStrategy.bind(scheduler)()
console.debug('seed with card_id=null', seed)

expect(f['_seed']).toBe(seed)
expect(f['_seed']).toBe(`${item.card.reps}`)
})
})

0 comments on commit 6098b02

Please sign in to comment.