diff --git a/src/scripts/main.js b/src/scripts/main.js index 49e4ad5b..9f3f1385 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1183,7 +1183,6 @@ import * as detailingFuncs from './modules/details/legacy'; "Iceberg": stagingFuncs.addIcebergStage, "Seasonal Garden": stagingFuncs.addSeasonalGardenStage, "Sunken City": stagingFuncs.addSunkenCityStage, - "Table of Contents": stagingFuncs.addTableOfContentsStage, "Toxic Spill": stagingFuncs.addToxicSpillStage, "Valour Rift": stagingFuncs.addValourRiftStage, "Whisker Woods Rift": stagingFuncs.addWhiskerWoodsRiftStage, diff --git a/src/scripts/modules/stages/environments/tableOfContents.ts b/src/scripts/modules/stages/environments/tableOfContents.ts new file mode 100644 index 00000000..7ce29acf --- /dev/null +++ b/src/scripts/modules/stages/environments/tableOfContents.ts @@ -0,0 +1,23 @@ +import {type User} from '@scripts/types/hg'; +import {type IntakeMessage} from '@scripts/types/mhct'; +import {type IStager} from '../stages.types'; + +export class TableOfContentsStager implements IStager { + readonly environment: string = 'Table of Contents'; + + addStage(message: IntakeMessage, userPre: User, userPost: User, journal: unknown): void { + const quest = userPre.quests.QuestTableOfContents; + + if (!quest) { + throw new Error('QuestTableOfContents is undefined'); + } + + if (quest.is_writing) { + message.stage = quest.current_book.volume > 0 + ? 'Encyclopedia' + : 'Pre-Encyclopedia'; + } else { + message.stage = 'Not Writing'; + } + } +} diff --git a/src/scripts/modules/stages/index.ts b/src/scripts/modules/stages/index.ts index 048d78f1..dbe38a83 100644 --- a/src/scripts/modules/stages/index.ts +++ b/src/scripts/modules/stages/index.ts @@ -21,6 +21,7 @@ import {QuesoGeyserStager} from './environments/quesoGeyser'; import {SandDunesStager} from './environments/sandDunes'; import {SlushyShorelineStager} from './environments/slushyShoreline'; import {SuperBrieFactoryStager} from './environments/superBrieFactory'; +import {TableOfContentsStager} from './environments/tableOfContents'; import {TwistedGardenStager} from './environments/twistedGarden'; import {ZokorStager} from './environments/zokor'; @@ -47,6 +48,7 @@ const stageModules: IStager[] = [ new SandDunesStager(), new SlushyShorelineStager(), new SuperBrieFactoryStager(), + new TableOfContentsStager(), new TwistedGardenStager(), new ZokorStager(), ]; diff --git a/src/scripts/modules/stages/legacy.js b/src/scripts/modules/stages/legacy.js index 5f76b224..d3cd4e2c 100644 --- a/src/scripts/modules/stages/legacy.js +++ b/src/scripts/modules/stages/legacy.js @@ -191,28 +191,6 @@ export function addFuromaRiftStage(message, user, user_post, hunt) { } } -/** - * Set the Table of Contents Stage - * @param {Object } message The message to be sent. - * @param {Object } user The user state object, when the hunt was invoked (pre-hunt). - * @param {Object } user_post The user state object, after the hunt. - * @param {Object } hunt The journal entry corresponding to the active hunt. - */ -export function addTableOfContentsStage(message, user, user_post, hunt) { - const quest = user.quests.QuestTableOfContents; - if (quest) { - if (quest.is_writing) { - if (quest.current_book.volume > 0) { - message.stage = 'Encyclopedia'; - } else { - message.stage = 'Pre-Encyclopedia'; - } - } else { - message.stage = 'Not Writing'; - } - } -} - /** * * @param {Object } message The message to be sent. diff --git a/tests/scripts/modules/stages/environments/tableOfContents.spec.ts b/tests/scripts/modules/stages/environments/tableOfContents.spec.ts new file mode 100644 index 00000000..b0cd8e43 --- /dev/null +++ b/tests/scripts/modules/stages/environments/tableOfContents.spec.ts @@ -0,0 +1,72 @@ + +import {TableOfContentsStager} from "@scripts/modules/stages/environments/tableOfContents"; +import {IStager} from "@scripts/modules/stages/stages.types"; +import {User} from "@scripts/types/hg"; +import {IntakeMessage} from "@scripts/types/mhct"; + +describe('Table of Contents stages', () => { + let stager: IStager; + + beforeEach(() => { + stager = new TableOfContentsStager(); + }); + + it('should be for the Table of Contents environment', () => { + expect(stager.environment).toBe('Table of Contents'); + }); + + it.each([undefined, null])('should throw when QuestTableOfContents is %p', (quest) => { + const message = {location: {}} as IntakeMessage; + const preUser = {quests: {QuestTableOfContents: quest}} as User; + const postUser = {} as User; + const journal = {}; + + expect(() => stager.addStage(message, preUser, postUser, journal)) + .toThrow('QuestTableOfContents is undefined'); + }); + + it('should set stage to Not Writing if user is not writing', () => { + const message = {} as IntakeMessage; + const preUser = {quests: {QuestTableOfContents: { + is_writing: false, + }}} as User; + const postUser = {} as User; + const journal = {}; + + stager.addStage(message, preUser, postUser, journal); + + expect(message.stage).toBe('Not Writing'); + }); + + it('should set stage to Pre-Encyclopedia when writing first volume', () => { + const message = {} as IntakeMessage; + const preUser = {quests: {QuestTableOfContents: { + is_writing: true, + current_book: { + volume: 0, + }, + }}} as User; + const postUser = {} as User; + const journal = {}; + + stager.addStage(message, preUser, postUser, journal); + + expect(message.stage).toBe('Pre-Encyclopedia'); + }); + + it('should set stage to Encyclopeda when writing after first volume', () => { + const message = {} as IntakeMessage; + const preUser = {quests: {QuestTableOfContents: { + is_writing: true, + current_book: { + volume: 1, + }, + }}} as User; + const postUser = {} as User; + const journal = {}; + + stager.addStage(message, preUser, postUser, journal); + + expect(message.stage).toBe('Encyclopedia'); + }); +});