diff --git a/__tests__/fixed/same-seed.test.ts b/__tests__/fixed/same-seed.test.ts new file mode 100644 index 0000000..c2a5c27 --- /dev/null +++ b/__tests__/fixed/same-seed.test.ts @@ -0,0 +1,45 @@ +import { createEmptyCard, fsrs, Rating } from '../../src/fsrs' + +describe('fuzz same seed', () => { + const MOCK_NOW = new Date(2024, 7, 15) + const size = 100 + + // https://github.com/open-spaced-repetition/ts-fsrs/issues/113 + it('should be the same[short-term]', () => { + const { card } = fsrs().next(createEmptyCard(), MOCK_NOW, Rating.Good) + const scheduler = fsrs({ enable_fuzz: true }) + const MOCK_TOMORROW = new Date(2024, 7, 16) + + const timestamp: number[] = [] + for (let count = 0; count < size; count++) { + setTimeout(() => { + const _card = scheduler.next(card, MOCK_TOMORROW, Rating.Good).card + timestamp.push(_card.due.getTime()) + if (timestamp.length === size) { + expect(timestamp.every((value) => value === timestamp[0])).toBe(true) + } + }, 50) + } + }) + + it('should be the same[long-term]', () => { + const { card } = fsrs({ enable_short_term: false }).next( + createEmptyCard(), + MOCK_NOW, + Rating.Good + ) + const scheduler = fsrs({ enable_fuzz: true, enable_short_term: false }) + const MOCK_TOMORROW = new Date(2024, 7, 18) + + const timestamp: number[] = [] + for (let count = 0; count < size; count++) { + setTimeout(() => { + const _card = scheduler.next(card, MOCK_TOMORROW, Rating.Good).card + timestamp.push(_card.due.getTime()) + if (timestamp.length === size) { + expect(timestamp.every((value) => value === timestamp[0])).toBe(true) + } + }, 50) + } + }) +}) diff --git a/package.json b/package.json index baa6fe9..de3cf11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-fsrs", - "version": "4.1.1", + "version": "4.1.2", "description": "ts-fsrs is a versatile package based on TypeScript that supports ES modules, CommonJS, and UMD. It implements the Free Spaced Repetition Scheduler (FSRS) algorithm, enabling developers to integrate FSRS into their flashcard applications to enhance the user learning experience.", "main": "dist/index.cjs", "umd": "dist/index.umd.js", diff --git a/src/fsrs/algorithm.ts b/src/fsrs/algorithm.ts index dbdae74..f1a772e 100644 --- a/src/fsrs/algorithm.ts +++ b/src/fsrs/algorithm.ts @@ -136,7 +136,7 @@ export class FSRSAlgorithm { **/ apply_fuzz(ivl: number, elapsed_days: number, enable_fuzz?: boolean): int { if (!enable_fuzz || ivl < 2.5) return Math.round(ivl) as int - const generator = alea(this.seed) + const generator = alea(this._seed) // I do not want others to directly access the seed externally. const fuzz_factor = generator() const { min_ivl, max_ivl } = get_fuzz_range( ivl, diff --git a/src/fsrs/default.ts b/src/fsrs/default.ts index 446b0d3..7b8ec90 100644 --- a/src/fsrs/default.ts +++ b/src/fsrs/default.ts @@ -11,7 +11,7 @@ export const default_w = [ export const default_enable_fuzz = false export const defualt_enable_short_term = true -export const FSRSVersion: string = 'v4.1.1 using FSRS V5.0' +export const FSRSVersion: string = 'v4.1.2 using FSRS V5.0' export const generatorParameters = ( props?: Partial