pokerogue/test/test-utils/text-interceptor.ts
Bertie690 9815c5eebf
[Test] Revamped MockConsoleLog with color support (#6218)
* Added mock console and fixed up many many things

* Cleaned up handling of colors and such

* Added minor comment

* Fix Focus Punch test

* Fix typo

* Remove redundant comment

* Update vitest.setup.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Added color map inside new folder

* Made constants not object bc i was told to

* Update src/constants/colors.ts

* Removed all moves init check

* Removed import

* Fixed up some stuff + added aquamarine color to settings helper

* Added logging for test end

* Removed intentionally failing test

* Fixed console log to use inheritance to not override vitest's wrapping

* Added a custom Vitest reporter to not log the test name every 2 lines

* Moved coloration to a hook to prevent misplacing things

* Fixed import issue by copypasting vitest soure

* Removed intentionally failing test

look i need to check that `test:silent` works on github ok

* Added REUSE annotations to copied parts of source

* Fixed import issue

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2025-09-07 09:18:47 -05:00

50 lines
1.5 KiB
TypeScript

import type { BattleScene } from "#app/battle-scene";
import chalk from "chalk";
/**
* The {@linkcode TextInterceptor} is a wrapper class that intercepts and logs any messages
* that would be displayed on-screen.
*/
export class TextInterceptor {
/** A log containing messages having been displayed on screen, sorted in FIFO order. */
public readonly logs: string[] = [];
constructor(scene: BattleScene) {
// @ts-expect-error: Find another more sanitary way of doing this
scene.messageWrapper = this;
}
/** Clear the current content of the TextInterceptor. */
public clearLogs(): void {
this.logs.splice(0);
}
showText(text: string): void {
// NB: We do not format the raw _logs_ themselves as tests will be actively checking it.
console.log(this.formatText(text));
this.logs.push(text);
}
showDialogue(text: string, name: string): void {
console.log(`${name}: \n${this.formatText(text)}`);
this.logs.push(name, text);
}
/**
* Format text to be displayed to the test console, as follows:
* 1. Replaces new lines and new text boxes (marked by `$`) with indented new lines.
* 2. Removes all `@c{}`, `@d{}`, `@s{}`, and `@f{}` flags from the text.
* 3. Makes text blue
* @param text - The unformatted text
* @returns The formatted text
*/
private formatText(text: string): string {
return chalk.blue(
text
.replace(/\n/g, " ")
.replace(/\$/g, "\n ")
.replace(/@\w{.*?}/g, ""),
);
}
}