diff options
| author | stonedDiscord <Tukz@gmx.de> | 2026-01-01 16:56:17 +0100 |
|---|---|---|
| committer | stonedDiscord <Tukz@gmx.de> | 2026-01-01 16:56:17 +0100 |
| commit | 01ecb948edb015613e05bc2b6da4021137957745 (patch) | |
| tree | e6c080d7e8e6f56652c2269a380eff40b899a065 | |
| parent | 26ae98b3d92f98754d5fb896713b524e5aca0483 (diff) | |
deal with this properly
| -rw-r--r-- | webAO/__tests__/aoml.test.ts | 75 | ||||
| -rw-r--r-- | webAO/utils/aoml.ts | 22 | ||||
| -rw-r--r-- | webAO/viewport/utils/handleICSpeaking.ts | 15 |
3 files changed, 43 insertions, 69 deletions
diff --git a/webAO/__tests__/aoml.test.ts b/webAO/__tests__/aoml.test.ts index 8cc4967..d612945 100644 --- a/webAO/__tests__/aoml.test.ts +++ b/webAO/__tests__/aoml.test.ts @@ -1,4 +1,4 @@ -const networkRequest = ` +const configIni = ` c0 = 247, 247, 247 c0_name = White c0_talking = 1 @@ -32,87 +32,56 @@ c6_remove = 0 c6_talking = 0 `; -// Mock the request module properly -jest.mock("../services/request", () => ({ - __esModule: true, - default: jest.fn().mockResolvedValue(networkRequest), - request: jest.fn().mockResolvedValue(networkRequest), - requestBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(0)) -})); - -import request from "../services/request"; import mlConfig from "../utils/aoml"; -// Ensure the mock is applied before any imports -beforeAll(() => { - jest.clearAllMocks(); - console.log("Mock applied:", request); -}); - -describe("mlConfig", () => { - beforeEach(() => { - // Clear all instances and calls to constructor and all methods: - jest.clearAllMocks(); - }); - - it("Should make a network request", () => { - mlConfig("/"); - expect(request).toHaveBeenCalledTimes(1); - }); -}); describe("applyMarkdown", () => { - const config = mlConfig("/"); - - beforeEach(() => { - // Clear all instances and calls to constructor and all methods: - jest.clearAllMocks(); - }); + const config = mlConfig(configIni); - it("Should create an array of spans containing letters", async () => { + it("Should create an array of spans containing letters", () => { const word = `hello`; - const actual = await config.applyMarkdown(`hello`, `blue`); + const actual = config.applyMarkdown(`hello`, `blue`); let index = 0; for (const element of actual) { expect(element.innerHTML).toBe(word[index]); index++; } }); - it("Should add colors based on settings", async () => { - const config = mlConfig("/"); - const actual = await config.applyMarkdown(`(heya)`, `blue`); + it("Should add colors based on settings", () => { + const config = mlConfig(configIni); + const actual = config.applyMarkdown(`(heya)`, `blue`); expect(actual[0].getAttribute("style")).toBe("color: rgb(107, 198, 247);"); }); - it("Should keep a letter if remove = 0", async () => { - const config = mlConfig("/"); + it("Should keep a letter if remove = 0", () => { + const config = mlConfig(configIni); - const actual = await config.applyMarkdown(`(What())Hey!`, `white`); + const actual = config.applyMarkdown(`(What())Hey!`, `white`); const expected = `(`; expect(actual[5].innerHTML).toBe(expected); }); - it("Should remove a letter if remove = 1", async () => { - const config = mlConfig("/"); + it("Should remove a letter if remove = 1", () => { + const config = mlConfig(configIni); - const actual = await config.applyMarkdown(`~What~()Hey!`, `white`); + const actual = config.applyMarkdown(`~What~()Hey!`, `white`); const expected = ``; expect(actual[0].innerHTML).toBe(expected); }); - it("Should remove a letter if remove = 1", async () => { - const config = mlConfig("/"); + it("Should remove a letter if remove = 1", () => { + const config = mlConfig(configIni); - const actual = await config.applyMarkdown(`~What~()Hey!`, `white`); + const actual = config.applyMarkdown(`~What~()Hey!`, `white`); const expected = ``; expect(actual[0].innerHTML).toBe(expected); }); - it("Should keep a closing letter if remove = 0", async () => { - const config = mlConfig("/"); + it("Should keep a closing letter if remove = 0", () => { + const config = mlConfig(configIni); - const actual = await config.applyMarkdown(`~NO[]~!`, `white`); + const actual = config.applyMarkdown(`~NO[]~!`, `white`); const expected = ``; expect(actual[4].innerHTML).toBe(expected); }); - it("Should remove a closing letter if remove = 1", async () => { - const config = mlConfig("/"); - const actual = await config.applyMarkdown(`~NO||~!`, `white`); + it("Should remove a closing letter if remove = 1", () => { + const config = mlConfig(configIni); + const actual = config.applyMarkdown(`~NO||~!`, `white`); const expected = ``; expect(actual[5].innerHTML).toBe(expected); }); diff --git a/webAO/utils/aoml.ts b/webAO/utils/aoml.ts index f4a6da5..7611399 100644 --- a/webAO/utils/aoml.ts +++ b/webAO/utils/aoml.ts @@ -1,4 +1,3 @@ -import request from "../services/request"; interface Aoml { [key: string]: string | number; @@ -33,15 +32,12 @@ const aomlParser = (text: string) => { return parsed; }; -const mlConfig = (AO_HOST: string) => { - const defaultUrl = `${AO_HOST}themes/default/chat_config.ini`; - const aomlParsed: Promise<{ [key: string]: Aoml }> = request(defaultUrl).then( - (data) => aomlParser(data), - ); +const mlConfig = (iniContent: string) => { + const aomlParsed: { [key: string]: Aoml } = aomlParser(iniContent); - const createIdentifiers = async () => { + const createIdentifiers = () => { const identifiers = new Map<string, Aoml>(); - for (const [ruleName, value] of Object.entries(await aomlParsed)) { + for (const [ruleName, value] of Object.entries(aomlParsed)) { if (value.start && value.end) { identifiers.set(value.start, value); identifiers.set(value.end, value); @@ -49,18 +45,18 @@ const mlConfig = (AO_HOST: string) => { } return identifiers; }; - const createStartIdentifiers = async () => { + const createStartIdentifiers = () => { const startingIdentifiers = new Set<string>(); - for (const [ruleName, value] of Object.entries(await aomlParsed)) { + for (const [ruleName, value] of Object.entries(aomlParsed)) { if (value?.start && value?.end) { startingIdentifiers.add(value.start); } } return startingIdentifiers; }; - const applyMarkdown = async (text: string, defaultColor: string) => { - const identifiers = await createIdentifiers(); - const startIdentifiers = await createStartIdentifiers(); + const applyMarkdown = (text: string, defaultColor: string) => { + const identifiers = createIdentifiers(); + const startIdentifiers = createStartIdentifiers(); const closingStack = []; const colorStack = []; // each value in output will be an html element diff --git a/webAO/viewport/utils/handleICSpeaking.ts b/webAO/viewport/utils/handleICSpeaking.ts index b037e69..c0a9ae1 100644 --- a/webAO/viewport/utils/handleICSpeaking.ts +++ b/webAO/viewport/utils/handleICSpeaking.ts @@ -11,8 +11,17 @@ import { resizeChatbox } from "../../dom/resizeChatbox"; import transparentPng from "../../constants/transparentPng"; import { COLORS } from "../constants/colors"; import mlConfig from "../../utils/aoml"; +import request from "../../services/request"; -const attorneyMarkdown = mlConfig(AO_HOST); +let attorneyMarkdown: ReturnType<typeof mlConfig> | null = null; + +const initAttorneyMarkdown = async () => { + if (!attorneyMarkdown) { + const iniContent = await request(`${AO_HOST}themes/default/chat_config.ini`); + attorneyMarkdown = mlConfig(iniContent); + } + return attorneyMarkdown; +}; export let startFirstTickCheck: boolean; export const setStartFirstTickCheck = (val: boolean) => { @@ -337,9 +346,9 @@ export const handle_ic_speaking = async (playerChatMsg: ChatMsg) => { } try { - client.viewport.getChatmsg().parsed = await attorneyMarkdown.applyMarkdown( + const markdown = await initAttorneyMarkdown(); + client.viewport.getChatmsg().parsed = markdown.applyMarkdown( client.viewport.getChatmsg().content, - COLORS[client.viewport.getChatmsg().color], ); } catch (error) { |
