Macros 2.0 (v0.3) - Replacing the existing Macro System with a new Macro Engine (#4820)

* Chevrotain lib and env setup

* First draft of the macro lexer

* fix ESLint types loading for chevrotain

* Cleaner lexer modes

* Readme link to Chevrotain & license

* Add jsconfig to tests folder

- Add jsconfig.json to tests folder, to prevent IDE errors on dynamic imports inside the page.evaluate execution.

* Slight improvements on lexer & first tests

* Add more lexer tests

* More edge cases tests

* Reorder tests

* Add macro execution modifiers + more tests

- Added macro flags (execution modifiers) to lexer
- Fixed some lexing issues
- Expanded lexer tests
- Treat lexer errors as failed test

* enable eslint for tests and run it

* Fix lexing unknown flags - treat as error

* Rewrote lexer modes/tokes to capture errors better

* Add lexing for output modifiers

* Clearer names for lexer tokens

* Increase tests default timeout

* Restructure lexer error testcases

* Allow legacy underscores in macro identifiers

* Test case for legacy single-colon syntax

* Improve lexer, removing warnings

* Basic setup for MacroParser + initial tests

* Make parser errors testable

* Add macros stuff to SillyTavern.getContext

* macros test case naming + lint

* Parser consumes basic macros

- Fix lexer mode names
- Add basic macro parsing (identifier, and arguments)
- Tests: basic macro parsing tests
- Tests: simplifyCstNode supports ignoring nodes, or flattening nodes to just plaintext

* Improve macro argument parsing to allow colons in values

Enhances separator handling by fixing separator type detection and enabling colon characters within argument values
Updates validation to require at least one argument component and adds error cases for empty arguments
Includes expanded test coverage for mixed separator scenarios and edge cases

* More nested macro tests

Add error case tests to enforce macro start position requirements
Include nested macro parsing scenarios and invalid syntax checks
Ensures parser correctly handles edge cases with embedded macros

* Unvendor chevrotain

* Add document rule

* Implement visitor, switch built-ins to new type

* Puppeteer -> playwright

* Revert "Implement visitor, switch built-ins to new type"

This reverts commit 706a94b4de62129df6bd6c25e2c6dec692d12226.

* Converted puppeteeer tests

* File rename

* chore: reduce Playwright worker count to 4 for performance/stability

* test: add comprehensive legacy macro parser test suite

- Added 13 test cases covering legacy macro formats (roll, reverse, comment, datetime, time_UTC, banned, setvar)
- Documented parser limitations with TODO comments for whitespace separators, special characters, and empty arguments
- Tests validate parsing of various separator styles (space, colon, +/-) and argument formats (quoted, numeric, empty)

* fix: handle legacy macro syntax with colon or whitespace separator

- Modified arguments rule to support both double-colon (::) and single-colon (:) separators
- Made single-colon separator optional to allow whitespace-separated legacy macros
- Removed TODO comments as parser now correctly handles legacy macro formats

* feat: support space-separated quoted arguments in macro parser

- Added parsing support for equals signs and quotes as valid argument tokens
- Removed TODO comments for legacy macro parsing with quoted arguments

* fix: improves macro argument parsing with colon handling

Enhances parser to correctly handle double colons within legacy single-colon arguments
Introduces separate parsing rules for arguments with different colon constraints
Adds test coverage for arguments containing double colons in legacy format

* fix: allow empty macro arguments after double-colon separator

- Changed argument rule from AT_LEAST_ONE to MANY to permit zero-length arguments
- Updated tests to verify empty argument parsing (e.g., `{{something::}}`)
- Enhanced simplifyCstNode helper with default flatten/ignore keys and improved null handling

* refactor: improve test documentation with inline macro display

- Added inline comments showing the actual macro syntax being tested for better readability
- Removed duplicate comment in error test case
- Cleaned up extra whitespace in legacy macro tests

* feat: add legacy macro preprocessing for time offset format

- Implemented preProcessFixLegacyMacros method to convert {{time_UTC±N}} to {{time::UTC±N}} format
- Updated tests to use new preprocessing step for legacy time macro parsing
- Added runPreProcessFix option to test helper functions for controlled legacy macro handling

* feat: add support for comment macros with double-slash syntax

- Added `DoubleSlash` token to lexer to recognize `//` as a valid macro identifier
- Updated parser to accept either `//` or standard identifiers as macro names
- Enhanced test suite with comprehensive comment macro test cases including multiline support

* feat: implement macro evaluation engine with CST walking and registry integration

(I'm tired, let's just throw this in right now)

- Added CST walker and macro registry to engine initialization
- Enhanced parseDocument to handle empty input, legacy macro preprocessing, and error collection
- Implemented async evaluate method with full macro resolution pipeline
- Added resolveMacro callback to handle unknown macros and registry execution
- Integrated lexing/parsing error handling with console warnings
- Added support for preserving unknown macro

* refactor: improve type safety and code clarity in macro evaluation system

- Simplified typedef imports to use correct Chevrotain types (CstNode, IToken)
- Added TokenRange typedef for consistent offset handling
- Enhanced error messages with context-specific prefixes
- Replaced verbose type casts with inline JSDoc annotations
- Condensed singleton pattern declarations to single lines
- Improved null safety with optional chaining and nullish coalescing
- Extracted resolveMacro logic into private root function

* fix test macro whitespace arguments onls accepting one argument

* Fix OpenRouter embeddings URL
#4736

* Fix: Prevent data loss on bulk regex move to scoped scripts (#4760)

* Fix: Prevent data loss in regex bulk move

* prevents moving to scoped scripts with group selected for bulkedit

* Refactor: make whitelist validation a bit more robust (#4757)

* refactor: extract IP whitelist validation into helper function

- Added isIPInWhitelist helper with error handling for individual whitelist entry checks
- Replaced inline whitelist matching logic with reusable function calls
- Added JSDoc type annotations and error logging for failed IP matching attempts

* refactor: simplify whitelist validation with upfront filtering

- Moved IP validation to startup time instead of per-request checking
- Extracted validateWhitelist function to filter invalid entries once at initialization
- Simplified isIPInWhitelist by removing redundant error handling after validation

* fix: correct IP whitelist matching to use parsed CIDR notation

* Feat: Improve multiline input handling in popups (#4756)

* feat: improve multiline input handling in popups

- Added Ctrl+Enter requirement for submission in multiline input popups to prevent accidental sends
- Exported PopupUtils class for external use

* refactor: remove redundant higher/different rows from input popups

- Removed rows: 2 from callGenericPopup calls where default behavior is sufficient
- Increased rows from 2 to 4 in caption extension for better multiline input experience

* Fix npm audit

* feat: add max-height and scrolling to world entry key input fields (#4769)

- Added 160px max-height to select2 multiple selection, primary key, and secondary key text areas in world entries
- Enabled vertical scrolling with hidden horizontal overflow for better UX with long content

* Bump anti-troll tags limit
Closes #4763

* OpenAI: Add Sora 2 API (#4748)

* OpenAI: Add Sora 2 API

* Add duration control

* Support client generation abort

* Reduce poll log amount

* Simplify selector

* Simplify model-specific control handling

* Gemini: Pass non-success response content to frontend

* Vertex: Add Vertex AI-specific safety setting (#4770)

Closes #4455

* Feature: allows sorting tags by most used (#4768)

* add sorting tags by most used

* Fix whitespaces

* Code review updates

* Remove commented code

* Fix capitalization in comment

* Apply review suggestion

* Simplify template init

* Reformat

* Add documentation for appendViewTagToList and printViewTagList functions

* Reprint renamed tags regardless of sorting mode

---------

Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

* Move img.swipe notice block up

* Preserve media playback state when running appendMediaToMessage (#4771)

* Preserve media playback state when running appendMediaToMessage

* Better selector specificity

* Fix local variable name

* Move typedef to global.d.ts

* Check for readyState on save/restore

* Only check for currentSrc on restoration callback

* gpt-5.1

* A 500 billion dollar startup can’t filter API payload fields

* Refactor macro argument validation to use requiredArgs and list pattern

- Replace minArgs/maxArgs/enforceArity with requiredArgs and list specification
- Add MacroListSpec typedef for flexible list argument constraints (min/max)
- Rename enforceArity to strictArgs with inverted default behavior
- Update validation logic to separately track required vs. list arguments
- Add requiredArgs and list arrays to MacroExecutionContext for easier access
- Improve error messages to clearly distinguish between required and list

* Refactor MacroRegistry.registerMacro to use options object pattern

- Move handler function into options object as required property
- Improve validation with detailed error messages for all option fields
- Add explicit type checking for requiredArgs, list, strictArgs, and description
- Consolidate name normalization and validation logic
- Simplify list option parsing with clearer conditional structure
- Update all builtin macro registrations to use new signature

* Add e2e tests for macro arity validation errors

- Test ping macro rejects calls with unexpected arguments
- Test upper macro rejects calls without required arguments
- Verify macros remain unresolved and log warnings on arity violations

* re-implement core macros as registered macros

- Create shallow frozen copy of env object before passing to macro handlers
- Add comprehensive MacroEnv typedef with nested types for names, character, system, and extra fields
- Update MacroDefinitionOptions typedef to clarify default values and mark handler as required
- Export variable manipulation functions for external use
- Fix whitespace in ifCallback JSDoc comment

* Defer slash command autocomplete initialization to firstLoadInit

- Move slash command autocomplete setup from module-level to initSlashCommandAutoComplete function
- Add null check for sendTextarea.value before accessing first character
- Import and call initSlashCommandAutoComplete in firstLoadInit sequence
- Export registerCoreMacros, registerInstructMacros, and registerVariableMacros using named export syntax
- Call registerCoreMacros from initMacros with

* Add variable manipulation functions to SillyTavern context API

- Import deleteGlobalVariable, deleteLocalVariable, addGlobalVariable, addLocalVariable, incrementGlobalVariable, incrementLocalVariable, decrementGlobalVariable, and decrementLocalVariable from variables.js
- Expose del, add, inc, and dec methods on both context.variables.local and context.variables.global objects
- Provide consistent API for variable deletion, addition, increment, and decrement operations

* Extract test setup utilities and fix trailing comma in core-macros.js

- Add testSetup utility object with goST and awaitST helper functions for Playwright tests
- Replace duplicated beforeEach setup code in MacroLexer, MacroParser, and MacroRegistry tests with testSetup.goST
- Add explanatory comments indicating tests currently run without ST context
- Fix missing trailing comma in input macro handler registration

* Refactor MacroEngine e2e tests to use real core macros instead of prototypes

- Replace prototype macros (ping, echo, upper, wrap, first) with actual core macros (newline, reverse, setvar, getvar, addvar, roll)
- Use testSetup.awaitST helper from utils.js for beforeEach setup
- Increase test timeout from 10s to 20s due to additional setup requirements
- Remove registerPrototypeMacros import and registration logic from evaluateWithEngine
- Add page.waitForTimeout(1000) to ensure macros are fully initialize
- Remove now obsolete MacroBuiltins.js

* Fix audit in tests

* Rename frontend test utils file to frontent-test-utils.js and update imports

- Rename tests/frontend/utils.js to tests/frontend/frontent-test-utils.js
- Update testSetup imports across MacroEngine, MacroLexer, MacroParser, and MacroRegistry e2e tests

* Refactor MacroRegistry for improved readability and consistency

- Reorder MacroDefinition typedef fields to match registration order (handler moved to end)
- Add inline JSDoc comments to MacroDefinitionOptions properties for clarity
- Simplify name validation logic in unregisterMacro, hasMacro, and getMacro methods using early returns
- Extract argument validation logic into dedicated isArgsValid helper function
- Refactor executeMacro to be async and use Promise.resolve for consistent promise handling

* Add returns field to MacroDefinitionOptions for documenting macro return values

- Add returns property to MacroDefinitionOptions typedef with string type and null default
- Include returns field in MacroDefinition typedef
- Store returns value in macro definition object for documentation purposes

* Add error handling to macro execution and refactor core macro handlers

- Add catch handler in MacroRegistry.executeMacro to log errors and return empty string on handler failures
- Update macro descriptions to use "index" instead of "ID" for message position macros
- Simplify reverse macro description and remove redundant null coalescing
- Add strictArgs: false to comment macro to ensure it's always removed
- Enhance time macro description with UTC offset examples
- Remove unnecessary null check

* Refactor instruct-macros.js for consistency and reduce code duplication

- Rename enabled helper to instEnabled and add sysEnabled helper for clarity
- Consolidate defaultSystemPrompt, instructSystem, and instructSystemPrompt into single registerSimple call
- Simplify systemPrompt handler by removing unnecessary null coalescing and intermediate variables
- Convert chatSeparator and chatStart macros to use registerSimple helper
- Fix JSDoc comment formatting for registerSimple helper function

* Add normalize helper to MacroExecutionContext and refactor variable macros for consistency

- Add normalizeMacroResult method to MacroEngine for converting macro results to strings
- Include normalize function in MacroExecutionContext for handler use
- Update normalizeMacroResult to handle arrays explicitly alongside objects
- Add returns field to variable macros that produce side-effects only
- Refactor variable macro handlers to use destructuring and normalize helper
- Remove redundant null checks an

* chore: Remove duplicate getglobalvar macro registration from variable-macros.js

- Remove redundant getglobalvar macro definition (already registered earlier in the file)
- Import MacroEngine in MacroRegistry for normalizeMacroResult access
- Add fallback binding for normalize in MacroExecutionContext when not provided by caller
- Update executeMacro to use executionContext.normalize instead of standalone normalizeMacroResult
- Remove normalizeMacroResult helper function from MacroRegistry (now handled by MacroEngine)

* Add core environment macros for names, character fields, system info, and deterministic pick

- Add lastGenerationType tracking with event listeners for GENERATION_STARTED and CHAT_CHANGED
- Add ensureLastGenerationTypeTracking helper to initialize event listeners once
- Register name macros: user, char, group, groupNotMuted, notChar, charIfNotGroup
- Register character card field macros: charPrompt, charInstruction, description, personality, scenario, persona, mesExamplesRaw, charDepthPrompt, cre

* Refactor core macros into separate modules by category (env, state, chat, time)

- Move name and character card field macros to new env-macros.js module
- Move system/device/runtime state macros to new state-macros.js module
- Move chat inspection macros (lastMessage, lastMessageId, etc.) to new chat-macros.js module
- Move time/date macros (time, date, weekday, isotime, etc.) to new time-macros.js module
- Remove lastGenerationType tracking logic and helper functions from core-macros.js (moved to state

* Reorganize macro system into engine and definitions directories

- Move macro engine components (MacroEngine, MacroRegistry, MacroLexer, MacroParser, MacroCstWalker) to macros/engine/ subdirectory
- Move macro definition modules (core-macros, env-macros, state-macros, chat-macros, time-macros, variable-macros, instruct-macros) to macros/definitions/ subdirectory
- Create macro-system.js as central entry point that exports engine singletons and initRegisterMacros function
- Refactor variable-macros.js to use S

* Export getGeneratingModel function and add MacroEnvBuilder to macro system exports

- Export getGeneratingModel function from script.js for external use
- Import MacroEnvBuilder in macro-system.js
- Add envBuilder singleton to macros export object alongside existing engine components

* Extract MacroEnv typedefs into separate MacroEnv.types.js file

- Create MacroEnv.types.js with MacroEnv, MacroEnvNames, MacroEnvCharacter, and MacroEnvSystem typedefs
- Remove MacroEnv typedef definitions from MacroRegistry.js
- Update MacroRegistry.js, MacroEngine.js, MacroEnvBuilder.js, and env-macros.js to import MacroEnv from MacroEnv.types.js
- Change MacroEngine.evaluate env parameter type from any to optional MacroEnv

* Add comprehensive e2e tests for MacroEnvBuilder

- Create MacroEnvBuilder.e2e.js with 13 test cases validating environment construction
- Test name override precedence (overrides vs global fallback)
- Test character field population based on replaceCharacterCard flag
- Test original value one-shot helper function behavior
- Test group override string propagation to group/group

* Add substituteParamsAsync function with experimental macro engine support

- Create substituteParamsAsync function in script.js as async alternative to substituteParams
- Use object destructuring pattern for function parameters following RO-RO convention
- Add experimental_macro_engine flag to power_user settings for feature gating
- Add MacroEnvFunctions typedef with original and postProcess function types to MacroEnv.types.js
- Add functions property to MacroEnv typedef for one-shot helpers an

* Add dynamic macro support to MacroEngine with environment-based override and postProcess execution

- Add defOverride parameter to MacroRegistry.executeMacro for temporary macro definitions
- Check env.dynamicMacros in MacroEngine#resolveMacro and create temporary macro definition when found
- Set strictArgs to true for dynamic macros to fail if called with arguments
- Execute env.functions.postProcess on macro results in MacroEngine#resolveMacro with error handling
- Update MacroEnv typedef to make

* Refactor macro handlers to assume non-null env and remove optional chaining

- Remove optional chaining (?.) from env property access in env-macros.js and instruct-macros.js
- Change MacroExecutionContext env property from optional to required in MacroRegistry.js
- Update executeMacro context parameter from optional to required
- Simplify original macro handler to call env.functions.original() directly without try-catch
- Update MacroHandler typedef to use arrow function syntax for consistency
- Remove redundant null checks an

* Refactor MacroRegistry.executeMacro to accept MacroCall object and make MacroExecutionContext properties non-optional

- Change executeMacro to accept MacroCall object instead of separate name and context parameters
- Move normalize function from context parameter to options object in executeMacro
- Construct MacroExecutionContext from MacroCall properties within executeMacro
- Set namedArgs to null in executionContext (currently unused)
- Update MacroEngine.resolveMacro to pass MacroCall directly

* Remove normalize parameter from MacroRegistry.executeMacro and bind normalizeMacroResult directly in execution context

- Remove normalize parameter from executeMacro options object in MacroRegistry.js
- Bind MacroEngine.normalizeMacroResult directly in executionContext instead of accepting override
- Remove normalize option from MacroEngine.resolveMacro call to executeMacro
- Add missing name fields (group, groupNotMuted, notChar) to MacroEnvBuilder default env object

* Make MacroEnvRawContext properties optional and change original function from required to optional in MacroEnvFunctions

- Change MacroEnvRawContext properties from required to optional with null defaults
- Change original function from required to optional in MacroEnvFunctions typedef
- Remove original function from default env object in MacroEnvBuilder (only include when provided)

* Refactor MacroEnvBuilder tests to use optional properties and add macro arity validation tests

- Remove explicit undefined/false assignments from MacroEnvRawContext test objects (now optional with defaults)
- Rename additionalMacro to dynamicMacros in MacroEnvBuilder tests and update property access from env.extra to env.dynamicMacros
- Move original function from env.extra to env.functions in test assertions
- Remove test for additionalMacro overriding original helper (no longer applicable with

* Improve macro error handling with dedicated diagnostics and runtime error propagation

- Import and use logMacroInternalError and logMacroRuntimeWarning from MacroDiagnostics in MacroEngine
- Wrap MacroRegistry.executeMacro call in try-catch to distinguish runtime vs internal errors
- Nest postProcess execution in inner try-catch with dedicated error logging
- Return raw macro syntax on execution failure instead of empty string
- Replace console.warn with logMacroRuntimeWarning for argument count

* Add macro argument type validation with positional argument definitions and runtime type checking

- Add MacroArgType and MacroPositionalArgDef typedefs for argument metadata
- Change requiredArgs option to accept number or MacroPositionalArgDef[] array
- Add requiredArgDefs property to MacroDefinition to store normalized argument definitions
- Validate requiredArgs array elements during macro registration (name, description, type fields)
- Generate default argument definitions when requiredArgs is a

* Add e2e tests for macro type validation and dynamic macro strict arity enforcement

- Add test verifying strict typed macros fail resolution when argument type is invalid
- Add test verifying non-strict typed macros execute with invalid types but log warnings
- Add test verifying dynamic macros reject arguments due to strictArgs enforcement
- Capture and assert runtime warning messages for type validation and arity violations
- Register test macros with integer type requirements and varying strict

* Add mesExamples macro with instruct mode formatting support

- Register mesExamples macro in env-macros.js to format dialogue examples
- Import parseMesExamples, main_api, power_user, and formatInstructModeExamples
- Check instruct mode enabled state and main_api to determine formatting path
- Parse raw examples using parseMesExamples with instruct mode flag
- Return empty string when raw examples are missing or parsed result is empty
- Format examples using formatInstructModeExamples when instruct mode is active

* Add URL navigation wait in test setup and increase Playwright worker count to 4

- Add waitForURL check after user selection in awaitST to ensure navigation completes before preloader check
- Increase Playwright workers from 1 to 4 for parallel test execution

* Add e2e tests for multi-line macro arguments and comment macro functionality

- Add test verifying reverse macro handles multi-line arguments with newline characters
- Add test verifying comment macro removes single-line comments with simple body
- Add test verifying comment macro accepts non-word characters immediately after //
- Add test verifying comment macro ignores additional // sequences inside comment body
- Add test verifying comment macro supports multi-line comment bodies

* Standardize JSDoc type annotations to use explicit null defaults and union types instead of nullable shorthand

* Remove individual test timeout configurations from frontend macro test files

* Add positional argument definitions with sample values and descriptions to core, env, and time macros

- Add sampleValue field to MacroPositionalArgDef typedef (optional string)
- Replace numeric requiredArgs with positional argument definition arrays in roll, banned, outlet, datetimeformat, and timeDiff macros
- Include name, sampleValue, description, and type fields for each positional argument
- Update timeDiff description to clarify absolute difference calculation
- Remove unnecessary blank lines in

* Remove async/await from macro engine evaluation and convert all macro handlers to synchronous execution

Makes me sad, but such is life

* Refactor substituteParams to use options object signature with backward compatibility for legacy positional arguments

- Rename original substituteParams to substituteParamsLegacy with unchanged positional argument signature
- Rename substituteParamsNew to substituteParams as the new primary function
- Add automatic detection and routing of legacy positional argument calls to substituteParamsLegacy
- Update substituteParamsExtended to use new options object signature and mark as deprecated

* forgor

* fix missing import, and package-lock, finally. Maybe.

* Add experimental macro engine toggle to UI settings

- Add experimental_macro_engine setting to default settings.json
- Move experimental_macro_engine property to correct position in power_user object (with other experimental settings)
- Add checkbox UI control in settings panel with flask icon and tooltip explaining nested macro resolution and logical replacement order
- Wire up checkbox event handler to save experimental_macro_engine setting
- Load experimental_macro_engine state on settings initialization

* Refactor macro pre/post-processing from parser to engine and improve error handling

- Move legacy macro pre-processing (time_UTC format) from MacroParser to MacroEngine #runPreProcessors
- Move trim macro post-processing to MacroEngine #runPostProcessors to handle cross-boundary behavior
- Remove MacroLexer import from MacroEngine (now handled by MacroParser.parseDocument)
- Update MacroParser.parseDocument to return separate lexingErrors and parserErrors arrays

* Add {{trim}} macro placeholder that defers to post-processing for cross-boundary whitespace handling

* Add eslint-plugin-playwright

* Add logMacroSyntaxWarning function for structured lexer/parser error reporting with compact human-readable payload

* Small code review fixes

- import event types from events.js
- Add chained fallback to {{input}}, just to be safe
- switched variables.js to export-per-method
- minor text adjustments in registered macro docs

* fix lexer not capturing linebreaks correctly & simplify plaintext token

- Replace alternation-based pattern with negated character class approach
- Use `(?:[^{]|\{(?!\{))+` to match non-brace chars or single braces not followed by another brace
- Add unicode flag for consistency
- Update comment to clarify intent: consume anything that is not the start of a macro '{{'

* Skip macro processing on char/group fields for env build (performance)

- Add returnRaw parameter to getCharacterCardFields to optionally return raw values without baseChatReplace
- Add returnRaw parameter to getGroupCharacterCards for consistent raw value handling
- Replace direct baseChatReplace calls with conditional transform function based on returnRaw flag
- Apply collapseNewlines when returnRaw is true and collapse_newlines setting is enabled
- Update MacroEnvBuilder to use the returnRaw as true

* Fix lexer failing to handle literal '{' before macro openers

- Add PlaintextOpenBrace token to lexer with pattern `/\{(?=\{\{)/` to match single '{' immediately before '{{'
- Update lexer mode definition to consume PlaintextOpenBrace before attempting macro start
- Update parser document rule to handle PlaintextOpenBrace as alternative to Plaintext
- Update MacroCstWalker to collect both Plaintext and Plaintext.OpenBrace tokens when building document items

* Add legacy non-curly marker pre-processing (<USER>, <BOT>, <CHAR>, <GROUP>, <CHARIFNOTGROUP>)

- Add pre-processing step in MacroEngine to rewrite legacy angle-bracket markers into their curly-brace macro equivalents
- Map <USER> → {{user}}, <BOT> → {{char}}, <CHAR> → {{char}}, <GROUP> → {{group}}, <CHARIFNOTGROUP> → {{charIfNotGroup}}
- Add e2e tests verifying legacy marker resolution through the engine pipeline
- Tests cover <USER>, <BOT>/<CHAR>, and <GROUP>/<CHARIFNOTGROUP> markers

* Add env.content to MacroEnv and use it for deterministic {{pick}} hashing

- Add content property to MacroEnv type definition and MacroEnvBuilder to expose the full original input string
- Update {{pick}} macro handler to use env.content hash instead of rawListString hash for seed generation
- Ensures deterministic behavior when the same prompt position contains different list items across evaluations
- Prevents {{pick}} from returning different values when nested macros resolve to different intermediate

* add e2e tests for env.content exposure and deterministic {{pick}} behavior

- Rename getChatIdHashCore → getChatIdHash in core-macros.js for consistency
- Add e2e test verifying env.content is exposed to macro handlers
- Add e2e test confirming {{pick}} returns stable results for same chat and content
- Tests verify deterministic behavior by comparing multiple evaluations with fixed chat_id_hash

* Rename chat macro helper functions to remove 'Core' suffix for consistency

* Add MacrosParser deprecation warnings and bridge to new macro engine when experimental flag enabled

- Import macroSystem and power_user for experimental macro engine integration
- Add @deprecated JSDoc tag to MacrosParser class
- Add #logDeprecated helper to warn about deprecated MacrosParser methods
- Add #registerMacroInNewEngine to bridge legacy macro registrations into new engine
- Add #unregisterMacroInNewEngine to bridge legacy macro unregistrations
- Log deprecation warnings in get, has, register

* Fix lint in tests

* Add comprehensive bracket handling tests and improve macro lexer/parser resilience to invalid syntax

- Add logMacroGeneralError function for non-macro-specific error logging
- Add CST validation check in MacroEngine to return original input if parser produces invalid CST
- Wrap MacroCstWalker.evaluateDocument in try-catch to gracefully handle evaluation failures
- Update MacroLexer Unknown token pattern to capture single closing braces not followed by another closing brace
- Add fallback mode exit

* Add error recovery for incomplete macros by flattening them to plaintext while preserving nested complete macros

- Enable Chevrotain error recovery in MacroParser constructor
- Add #isRecoveryToken helper to detect tokens inserted during error recovery
- Add #flattenIncompleteMacro to recursively convert incomplete macro nodes into plaintext items
- Update #collectDocumentItems to detect recovery-inserted Macro.End tokens and flatten incomplete macros
- Simplify plaintext token collection to use

* Add e2e test verifying nested macros resolve even when outer macro has invalid argument count

* Remove unused macro CST node caching mechanism from MacroCstWalker

* Add JSDoc type re-exports and register shorthand to macro-system.js for improved DX

- Re-export commonly used JSDoc types from MacroRegistry and MacroEnv modules for easier consumption by external code
- Add macros.register shorthand function bound to MacroRegistry.registerMacro for convenient macro registration
- Includes MacroDefinitionOptions, MacroHandler, MacroEnv, and related type definitions

* Migrate legacy macro registrations to new macro system API across multiple modules

- Replace MacrosParser.registerMacro calls with macros.register using object-based configuration
- Update imports from './macros.js' to './macros/macro-system.js'
- Extract macro registration into dedicated registerAuthorsNoteMacros function in authors-note.js
- Add descriptions to all macro registrations for better documentation
- Update MacrosParser iterator to yield from new registry when experimental engine is enabled

* Add MacroCategory to all macro registrations across codebase for improved organization and discoverability

- Import MacroCategory from macro-system.js in authors-note.js, memory/index.js, stable-diffusion/index.js, and macros.js
- Add category property to all macro registrations using appropriate MacroCategory values
- Assign 'legacy' category to MacrosParser auto-registered macros
- Categorize macros across chat, character, prompts, utility, random, state, time, and names categories

* fix lint

* Add MacroBrowser UI component and integrate macro help system into chat interface

- Add implementation of MacroBrowser
- Add macros.css stylesheet link to index.html
- Update core-macros.js ban macro to return empty string instead of 'Empty string' description
- Add sampleValue property to positional argument definitions in MacroRegistry
- Generate default sampleValue for numeric requiredArgs using 'arg{n}' pattern
- Set default description to '<no description>' and returns to '<empty string>' when empty

* Reorder JSDoc typedef declarations in MacroRegistry.js to have the most relevant first

* fix missing category in legacy initMacros

* Add displayOverride and exampleUsage properties to macro registration system

- Add displayOverride property to MacroDefinitionOptions for custom signature display
- Add exampleUsage property to MacroDefinitionOptions for documentation examples
- Update MacroRegistry.registerMacro to validate and process displayOverride and exampleUsage
- Add logMacroRegisterWarning function to MacroDiagnostics for registration-time warnings
- Auto-wrap displayOverride and exampleUsage in curly braces if missing

* Add error handling to MacroRegistry.registerMacro and logMacroRegisterError diagnostic function

- Add logMacroRegisterError function to MacroDiagnostics for registration failures
- Wrap MacroRegistry.registerMacro body in try-catch to handle registration errors gracefully
- Change registerMacro return type from MacroDefinition to MacroDefinition|null
- Log registration errors and return null instead of throwing, preventing macro registration failures from breaking the application

* Optimize character card field access with lazy evaluation to improve macro execution performance

* Add brace unescaping to MacroEngine post-processing to support literal curly braces in macro output

- Add regex replacement to unescape \{ and \} to { and } after macro execution
- Allows users to output literal braces by escaping them with backslashes
- Escaped sequences like \{\{ don't match MacroStart pattern and pass through as plain text

* Add alias system to macro registry with UI support for displaying and navigating macro aliases

- Add `aliases` property to MacroDefinitionOptions for defining alternative macro names
- Add `aliasOf` and `aliasVisible` properties to MacroDefinition to track alias relationships
- Update MacroRegistry.registerMacro to create alias entries pointing to primary definitions
- Add `getPrimaryMacro` method to retrieve primary definition from alias names
- Add filtering options to `getAllMacros` to exclude aliases

* Strip curly braces from MacroBrowser search query to match macro name format in search definitions

* Add enhanced macro autocomplete with argument hints and context-aware suggestions

- Add comprehensive CSS styling for enhanced macro autocomplete items with flex layout
- Add argument hint banner styling with gradient background and border
- Add current argument highlighting in details panel
- Export formatMacroSignature, createSourceIndicator, createAliasIndicator, and createTypeBadge from MacroBrowser for reuse

* Fix macro autocomplete not showing details/arguments because of trailing colons

* Change macro details CSS selectors from `.macroBrowser` to `.macro-details` for better reusability and update autocomplete to use macro enum icon

- Replace all `.macroBrowser` selectors with `.macro-details` in macros.css to allow macro details panel styling to work outside MacroBrowser context
- Change autocomplete option icon from hardcoded '{}' to `enumIcons.macro` for consistency
- Remove redundant `showCategory: false` option from renderMacroDetails call in autocomplete (now handled by default

* Add MacroArgType enum to replace string literal type union for macro argument types

- Add MacroArgType enum with STRING, INTEGER, NUMBER, and BOOLEAN values
- Replace MacroArgType typedef string literal union with enum reference
- Export MacroArgType from macro-system.js alongside MacroCategory
- Remove MacroArgType typedef re-export (now an enum, not a type)

* Add support for multiple argument types in macro definitions

- Update createTypeBadge to handle both single type and array of types, displaying as "type1 | type2" with tooltip
- Add JSDoc comments to MacroArgType enum values explaining each type
- Update MacroArgDefinition typedef to allow type property to be single MacroArgType or array
- Update MacroRegistry.registerMacro to validate array of types and default empty arrays to 'string'
- Update validateArgTypes to check if argument value matches

* Add returnType property to macro definitions with automatic type badge display in macro details panel

- Rename MacroArgType enum to MacroValueType to reflect dual use for arguments and return types
- Add returnType property to MacroDefinitionOptions (defaults to MacroValueType.STRING)
- Add returnType validation in MacroRegistry.registerMacro to ensure valid type values
- Update renderMacroDetails to always show Returns section with type badge
- Add macro-returns-content CSS class with flex layout for type

* Add detailed argument definitions, return types, and example usage to all variable macros

- Replace numeric requiredArgs with detailed argument definition objects including name, type, and description
- Add returnType property to all macros that return values (inc/dec/get variants)
- Add returns property descriptions to all macros
- Add exampleUsage arrays demonstrating typical usage patterns for each macro
- Apply changes consistently

* Add returns descriptions and return types to core macro definitions with improved documentation

* Add returns descriptions, display overrides, and example usage to time macros with improved documentation

* fix lint

* Add returns descriptions and return types to chat, environment, instruct, and state macros with improved documentation

* Add missing properties to dynamic macro definition override to match MacroDefinitionOptions structure

* Replace console logging with MacroDiagnostics logging in MacroEnvBuilder and MacroRegistry

* Add support for array-based argument types in macro autocomplete with union type display and tooltip

* Rename requiredArgs to unnamedArgs and add support for optional unnamed arguments with bracket notation in macro signatures and hints

* Add optional offset argument definition to time macro with type, sample value, and description

* Simplify example usage for random and pick macros by removing surrounding context text

* Add default value display for optional macro arguments in autocomplete and browser documentation

* Fix macro args defaultValue not being converted into the normalized values for register and display

* Allow STscript macro auto completion to still show up when typing closing braces

* Add space macro with optional count argument for inserting multiple spaces

* Add optional count argument to newline macro for inserting multiple newlines with default value of 1

* Remove MutationObserver and CTRL+F keyboard event handling from MacroBrowser

* fix lint

* Register `{{summary}}` macro for both old and new macro engines based on experimental flag

* Register `{{charPrefix}}` and `{{charNegativePrefix}}` macros for both old and new macro engines based on experimental flag

* Update hidden alias badge text to indicate deprecation status

* Simplify macro name validation and error handling in MacroRegistry

Remove redundant `macroName` variable by normalizing `name` parameter early and reusing it throughout the registration flow. Consolidate trim checks in validation condition.

* Remove exp macro engine flag checks from auto complete and help, and remove legacy macro template

Move macro registration to always use new engine regardless of experimental flag. Remove conditional logic for `experimental_macro_engine` in MacroBrowser, system messages, and slash command parser. Delete legacy `macros.html` template and associated static macro help generation. Always use MacroBrowser for macro documentation display.

* Rename macros to camelCase and add backward-compatible aliases

Rename `description`, `personality`, `scenario`, `creatorNotes` to `charDescription`, `charPersonality`, `charScenario`, `charCreatorNotes` respectively. Rename `idle_duration` to `idleDuration`. Add old names as aliases for backward compatibility. Add `comment` as visible alias for `//` macro. Mark `idle_duration` alias as hidden.

* fix `random` and `pick` macros by using list parameter directly instead of raw string

Remove `raw` parameter from `random` and `pick` macro handlers. Simplify legacy comma-separated list handling by using `list[0]` directly instead of `rawListString`. Rename `items` variable to `list` in `pick` macro for consistency.

* fix `random` and `pick` not handling all colon-separated lists as before

Extract `readSingleArgsRandomList` helper function to handle legacy comma-separated and double-colon list parsing. Reuse this helper in both `random` and `pick` macro handlers to eliminate duplicated list parsing logic.

---------

Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
Co-authored-by: bmen25124 <bmen25124@gmail.com>
This commit is contained in:
Wolfsblvt
2025-12-15 20:29:46 +01:00
committed by GitHub
parent 4f9aeac7ac
commit 6f8b6b098e
45 changed files with 8505 additions and 275 deletions
+748
View File
@@ -0,0 +1,748 @@
import { test, expect } from '@playwright/test';
import { testSetup } from './frontent-test-utils.js';
test.describe('MacroEngine', () => {
test.beforeEach(testSetup.awaitST);
test.describe('Basic evaluation', () => {
test('should return input unchanged when there are no macros', async ({ page }) => {
const input = 'Hello world, no macros here.';
const output = await evaluateWithEngine(page, input);
expect(output).toBe(input);
});
test('should evaluate a simple macro without arguments', async ({ page }) => {
const input = 'Start {{newline}} end.';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Start \n end.');
});
test('should evaluate multiple macros in order', async ({ page }) => {
const input = 'A {{setvar::test::4}}{{getvar::test}} B {{setvar::test::2}}{{getvar::test}} C';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('A 4 B 2 C');
});
});
test.describe('Unnamed arguments', () => {
test('should handle normal double-colon separated unnamed argument', async ({ page }) => {
const input = 'Reversed: {{reverse::abc}}!';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Reversed: cba!');
});
test('should handle (legacy) colon separated unnamed argument', async ({ page }) => {
const input = 'Reversed: {{reverse:abc}}!';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Reversed: cba!');
});
test('should handle (legacy) colon separated argument as only one, even with more separators (double colon)', async ({ page }) => {
const input = 'Reversed: {{reverse:abc::def}}!';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Reversed: fed::cba!');
});
test('should handle (legacy) colon separated argument as only one, even with more separators (single colon)', async ({ page }) => {
const input = 'Reversed: {{reverse:abc:def}}!';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Reversed: fed:cba!');
});
test('should handle (legacy) whitespace separated unnamed argument', async ({ page }) => {
const input = 'Values: {{roll 1d1}}!';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Values: 1!');
});
test('should handle (legacy) whitespace separated unnamed argument as only one, even with more separators (space)', async ({ page }) => {
const input = 'Values: {{reverse abc def}}!';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Values: fed cba!');
});
test('should support multi-line arguments for macros', async ({ page }) => {
const input = 'Result: {{reverse::first line\nsecond line}}'; // "\n" becomes a real newline in the macro argument
const output = await evaluateWithEngine(page, input);
const original = 'first line\nsecond line';
const expectedReversed = Array.from(original).reverse().join('');
expect(output).toBe(`Result: ${expectedReversed}`);
});
});
test.describe('Nested macros', () => {
test('should resolve nested macros inside arguments inside-out', async ({ page }) => {
const input = 'Result: {{setvar::test::0}}{{reverse::{{addvar::test::100}}{{getvar::test}}}}{{setvar::test::0}}';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Result: 001');
});
// {{wrap::{{upper::x}}::[::]}} -> '[X]'
test('should resolve nested macros across multiple arguments', async ({ page }) => {
const input = 'Result: {{setvar::addvname::test}}{{addvar::{{getvar::addvname}}::{{setvar::test::5}}{{getvar::test}}}}{{getvar::test}}';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Result: 10');
});
});
test.describe('Unknown macros', () => {
test('should keep unknown macro syntax but resolve nested macros inside it', async ({ page }) => {
const input = 'Test: {{unknown::{{newline}}}}';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Test: {{unknown::\n}}');
});
test('should keep surrounding text inside unknown macros intact', async ({ page }) => {
const input = 'Test: {{unknown::my {{newline}} example}}';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Test: {{unknown::my \n example}}');
});
});
test.describe('Comment macro', () => {
test('should remove single-line comments with simple body', async ({ page }) => {
const input = 'Hello{{// comment}}World';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('HelloWorld');
});
test('should accept non-word characters immediately after //', async ({ page }) => {
const input = 'A{{//!@#$%^&*()_+}}B';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('AB');
});
test('should ignore additional // sequences inside the comment body', async ({ page }) => {
const input = 'X{{//comment with // extra // slashes}}Y';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('XY');
});
test('should support multi-line comment bodies', async ({ page }) => {
const input = 'Start{{// line one\nline two\nline three}}End';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('StartEnd');
});
});
test.describe('Legacy compatibility', () => {
test('should strip trim macro and surrounding newlines (legacy behavior)', async ({ page }) => {
const input = 'foo\n\n{{trim}}\n\nbar';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('foobar');
});
test('should handle multiple trim macros in a single string', async ({ page }) => {
const input = 'A\n\n{{trim}}\n\nB\n\n{{trim}}\n\nC';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('ABC');
});
test('should support legacy time macro with positive offset via pre-processing', async ({ page }) => {
const input = 'Time: {{time_UTC+2}}';
const output = await evaluateWithEngine(page, input);
// After pre-processing, this should behave like {{time::UTC+2}} and be resolved by the time macro.
// We only assert that the placeholder was consumed and some non-empty value was produced.
expect(output).not.toBe(input);
expect(output.startsWith('Time: ')).toBeTruthy();
expect(output.length).toBeGreaterThan('Time: '.length);
});
test('should support legacy time macro with negative offset via pre-processing', async ({ page }) => {
const input = 'Time: {{time_UTC-10}}';
const output = await evaluateWithEngine(page, input);
expect(output).not.toBe(input);
expect(output.startsWith('Time: ')).toBeTruthy();
expect(output.length).toBeGreaterThan('Time: '.length);
});
test('should support legacy <USER> marker via pre-processing', async ({ page }) => {
const input = 'Hello <USER>!';
const output = await evaluateWithEngine(page, input);
// In the default test env, name1Override is "User".
expect(output).toBe('Hello User!');
});
test('should support legacy <BOT> and <CHAR> markers via pre-processing', async ({ page }) => {
const input = 'Bot: <BOT>, Char: <CHAR>.';
const output = await evaluateWithEngine(page, input);
// In the default test env, name2Override is "Character".
expect(output).toBe('Bot: Character, Char: Character.');
});
test('should support legacy <GROUP> and <CHARIFNOTGROUP> markers via pre-processing (non-group fallback)', async ({ page }) => {
const input = 'Group: <GROUP>, CharIfNotGroup: <CHARIFNOTGROUP>.';
const output = await evaluateWithEngine(page, input);
// Without an active group, both markers fall back to the current character name.
expect(output).toBe('Group: Character, CharIfNotGroup: Character.');
});
});
test.describe('Bracket handling around macros', () => {
test('should allow single opening brace inside macro arguments', async ({ page }) => {
const input = 'Test§ {{reverse::my { test}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
// "my { test" reversed becomes "tset { ym"
expect(output).toBe('Test§ tset { ym');
const EXPECT_WARNINGS = false;
const EXPECT_ERRORS = false;
expect(hasMacroWarnings).toBe(EXPECT_WARNINGS);
expect(hasMacroErrors).toBe(EXPECT_ERRORS);
});
test('should allow single closing brace inside macro arguments', async ({ page }) => {
const input = 'Test§ {{reverse::my } test}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
// "my } test" reversed becomes "tset } ym"
expect(output).toBe('Test§ tset } ym');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should treat unterminated macro with identifier at end of input as plain text', async ({ page }) => {
const input = 'Test {{ hehe';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe(input);
expect(hasMacroWarnings).toBe(true);
expect(hasMacroErrors).toBe(false);
});
test('should treat invalid macro start as plain text when followed by non-identifier characters', async ({ page }) => {
const input = 'Test {{§§ hehe';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe(input);
expect(hasMacroWarnings).toBe(false); // Doesn't even try to recognize this as a macro, doesn't look like one. No warning is fine
expect(hasMacroErrors).toBe(false);
});
test('should treat unterminated macro in the middle of the string as plain text', async ({ page }) => {
const input = 'Before {{ hehe After';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe(input);
expect(hasMacroWarnings).toBe(true);
expect(hasMacroErrors).toBe(false);
});
test('should treat dangling macro start as text and still evaluate subsequent macro', async ({ page }) => {
const input = 'Test {{ hehe {{user}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
// Default test env uses name1Override = "User" and name2Override = "Character".
expect(output).toBe('Test {{ hehe User');
expect(hasMacroWarnings).toBe(true);
expect(hasMacroErrors).toBe(false);
});
test('should ignore invalid macro start but still evaluate following valid macro', async ({ page }) => {
const input = 'Test {{&& hehe {{user}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
// Default test env uses name1Override = "User" and name2Override = "Character".
expect(output).toBe('Test {{&& hehe User');
expect(hasMacroWarnings).toBe(false); // Doesn't even try to recognize this as a macro, doesn't look like one. No warning is fine
expect(hasMacroErrors).toBe(false);
});
test('should allow single opening brace immediately before a macro', async ({ page }) => {
const input = '{{{char}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
// One literal '{' plus the resolved character name.
expect(output).toBe('{Character');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should allow single closing brace immediately after a macro', async ({ page }) => {
const input = '{{char}}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('Character}');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should allow single braces around a macro', async ({ page }) => {
const input = '{{{char}}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('{Character}');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should allow double opening braces immediately before a macro', async ({ page }) => {
const input = '{{{{char}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('{{Character');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should allow double closing braces immediately after a macro', async ({ page }) => {
const input = '{{char}}}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('Character}}');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should allow double braces around a macro', async ({ page }) => {
const input = '{{{{char}}}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('{{Character}}');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should resolve nested macro inside argument with surrounding braces', async ({ page }) => {
const input = 'Result: {{reverse::pre-{ {{user}} }-post}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
// Argument "pre-{ User }-post" reversed becomes "tsop-} resU {-erp".
expect(output).toBe('Result: tsop-} resU {-erp');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should handle adjacent macros with no separator', async ({ page }) => {
const input = '{{char}}{{user}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('CharacterUser');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should handle macros separated only by surrounding braces', async ({ page }) => {
const input = '{{char}}{ {{user}} }';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('Character{ User }');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should handle Windows newlines with braces near macros', async ({ page }) => {
const input = 'Line1 {{char}}\r\n{Line2}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('Line1 Character\r\n{Line2}');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should treat stray closing braces outside macros as plain text', async ({ page }) => {
const input = 'Foo }} bar';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe(input);
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should keep stray closing braces and still evaluate following macro', async ({ page }) => {
const input = 'Foo }} {{user}}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('Foo }} User');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
test('should handle stray closing braces before macros as plain text', async ({ page }) => {
const input = 'Foo {{user}} }}';
const { output, hasMacroWarnings, hasMacroErrors } = await evaluateWithEngineAndCaptureMacroLogs(page, input);
expect(output).toBe('Foo User }}');
expect(hasMacroWarnings).toBe(false);
expect(hasMacroErrors).toBe(false);
});
});
test.describe('Arity errors', () => {
test('should not resolve newline when called with arguments', async ({ page }) => {
/** @type {string[]} */
const warnings = [];
page.on('console', msg => {
if (msg.type() === 'warning') {
warnings.push(msg.text());
}
});
const input = 'Start {{newline::extra}} end.';
const output = await evaluateWithEngine(page, input);
// Macro text should remain unchanged
expect(output).toBe(input);
// Should have logged an arity warning for newline
expect(warnings.some(w => w.includes('Macro "newline"') && w.includes('unnamed arguments'))).toBeTruthy();
});
test('should not resolve reverse when called without arguments', async ({ page }) => {
/** @type {string[]} */
const warnings = [];
page.on('console', msg => {
if (msg.type() === 'warning') {
warnings.push(msg.text());
}
});
const input = 'Result: {{reverse}}';
const output = await evaluateWithEngine(page, input);
expect(output).toBe(input);
expect(warnings.some(w => w.includes('Macro "reverse"') && w.includes('unnamed arguments'))).toBeTruthy();
});
test('should not resolve reverse when called with too many arguments', async ({ page }) => {
/** @type {string[]} */
const warnings = [];
page.on('console', msg => {
if (msg.type() === 'warning') {
warnings.push(msg.text());
}
});
const input = 'Result: {{reverse::a::b}}';
const output = await evaluateWithEngine(page, input);
// Macro text should remain unchanged when extra unnamed args are provided
expect(output).toBe(input);
// Should have logged an arity warning for reverse
expect(warnings.some(w => w.includes('Macro "reverse"') && w.includes('unnamed arguments'))).toBeTruthy();
});
test('should not resolve list-bounded macro when called outside list bounds', async ({ page }) => {
/** @type {string[]} */
const warnings = [];
page.on('console', msg => {
if (msg.type() === 'warning') {
warnings.push(msg.text());
}
});
// Register a temporary macro with explicit list bounds: exactly 1 required + 1-2 list args
await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
MacroRegistry.unregisterMacro('test-list-bounds');
MacroRegistry.registerMacro('test-list-bounds', {
unnamedArgs: 1,
list: { min: 1, max: 2 },
description: 'Test macro for list bounds.',
handler: ({ unnamedArgs, list }) => {
const all = [...unnamedArgs, ...(list ?? [])];
return all.join('|');
},
});
});
// First macro: too few list args (only required arg)
// Second macro: too many list args (required arg + 3 list entries)
const input = 'A {{test-list-bounds::base}} B {{test-list-bounds::base::x::y::z}}';
const output = await evaluateWithEngine(page, input);
// Both macros should remain unchanged in the output
expect(output).toBe(input);
const testWarnings = warnings.filter(w => w.includes('Macro "test-list-bounds"') && w.includes('unnamed arguments'));
// We expect one warning for each invalid invocation (too few and too many list args)
expect(testWarnings.length).toBe(2);
});
test('should resolve nested macros in arguments, even though the outer macro has wrong number of arguments', async ({ page }) => {
// Macro {{user ....}} will fail, because it has no args, but {{char}} should still resolve
const input = 'Result: {{user Something {{char}}}}';
const output = await evaluateWithEngine(page, input);
expect(output).toBe('Result: {{user Something Character}}');
});
});
test.describe('Type validation', () => {
test('should not resolve strict typed macro when argument type is invalid', async ({ page }) => {
/** @type {string[]} */
const warnings = [];
page.on('console', msg => {
if (msg.type() === 'warning') {
warnings.push(msg.text());
}
});
await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
MacroRegistry.unregisterMacro('test-int-strict');
MacroRegistry.registerMacro('test-int-strict', {
unnamedArgs: [
{ name: 'value', type: 'integer', description: 'Must be an integer.' },
],
strictArgs: true,
description: 'Strict integer macro for testing type validation.',
handler: ({ unnamedArgs: [value] }) => `#${value}#`,
});
});
const input = 'Value: {{test-int-strict::abc}}';
const output = await evaluateWithEngine(page, input);
// Strict typed macro should leave the text unchanged when the argument is invalid
expect(output).toBe(input);
// A runtime type validation warning should be logged
expect(warnings.some(w => w.includes('Macro "test-int-strict"') && w.includes('expected type integer'))).toBeTruthy();
});
test('should resolve non-strict typed macro when argument type is invalid but still log warning', async ({ page }) => {
/** @type {string[]} */
const warnings = [];
page.on('console', msg => {
if (msg.type() === 'warning') {
warnings.push(msg.text());
}
});
await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
MacroRegistry.unregisterMacro('test-int-nonstrict');
MacroRegistry.registerMacro('test-int-nonstrict', {
unnamedArgs: [
{ name: 'value', type: 'integer', description: 'Must be an integer.' },
],
strictArgs: false,
description: 'Non-strict integer macro for testing type validation.',
handler: ({ unnamedArgs: [value] }) => `#${value}#`,
});
});
const input = 'Value: {{test-int-nonstrict::abc}}';
const output = await evaluateWithEngine(page, input);
// Non-strict typed macro should still execute, even with invalid type
expect(output).toBe('Value: #abc#');
// A runtime type validation warning should still be logged
expect(warnings.some(w => w.includes('Macro "test-int-nonstrict"') && w.includes('expected type integer'))).toBeTruthy();
});
});
test.describe('Environment', () => {
test('should expose original content as env.content to macro handlers', async ({ page }) => {
const input = '{{env-content}}';
const originalContent = 'This is the full original input string.';
const output = await page.evaluate(async ({ input, originalContent }) => {
/** @type {import('../../public/scripts/macros/engine/MacroEngine.js')} */
const { MacroEngine } = await import('./scripts/macros/engine/MacroEngine.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
MacroRegistry.unregisterMacro('env-content');
MacroRegistry.registerMacro('env-content', {
description: 'Test macro that returns env.content.',
handler: ({ env }) => env.content,
});
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const rawEnv = {
content: originalContent,
};
const env = MacroEnvBuilder.buildFromRawEnv(rawEnv);
return MacroEngine.evaluate(input, env);
}, { input, originalContent });
expect(output).toBe(originalContent);
});
});
test.describe('Deterministic pick macro', () => {
test('should return stable results for the same chat and content', async ({ page }) => {
// Simulate a consistent chat id hash
let originalHash;
await page.evaluate(async ([originalHash]) => {
/** @type {import('../../public/script.js')} */
const { chat_metadata } = await import('./script.js');
originalHash = chat_metadata['chat_id_hash'];
chat_metadata['chat_id_hash'] = 123456;
}, [originalHash]);
const input = 'Choices: {{pick::red::green::blue}}, {{pick::red::green::blue}}.';
const output1 = await evaluateWithEngine(page, input);
const output2 = await evaluateWithEngine(page, input);
// Deterministic: same chat and same content should yield identical output.
expect(output1).toBe(output2);
// Sanity check: both picks should resolve to one of the provided options.
const match = output1.match(/Choices: ([^,]+), ([^.]+)\./);
expect(match).not.toBeNull();
if (!match) return;
const first = match[1].trim();
const second = match[2].trim();
const options = ['red', 'green', 'blue'];
expect(options.includes(first)).toBeTruthy();
expect(options.includes(second)).toBeTruthy();
// Restore original hash
await page.evaluate(async ([originalHash]) => {
/** @type {import('../../public/script.js')} */
const { chat_metadata } = await import('./script.js');
chat_metadata['chat_id_hash'] = originalHash;
}, [originalHash]);
});
});
test.describe('Dynamic macros', () => {
test('should not resolve dynamic macro when called with arguments due to strict arity', async ({ page }) => {
/** @type {string[]} */
const warnings = [];
page.on('console', msg => {
if (msg.type() === 'warning') {
warnings.push(msg.text());
}
});
const input = 'Dyn: {{dyn::extra}}';
const output = await page.evaluate(async (input) => {
/** @type {import('../../public/scripts/macros/engine/MacroEngine.js')} */
const { MacroEngine } = await import('./scripts/macros/engine/MacroEngine.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const rawEnv = {
content: input,
dynamicMacros: {
dyn: () => 'OK',
},
};
const env = MacroEnvBuilder.buildFromRawEnv(rawEnv);
return MacroEngine.evaluate(input, env);
}, input);
// Dynamic macro with arguments should not resolve because the
// temporary definition is strictArgs: true and minArgs/maxArgs: 0.
expect(output).toBe(input);
// A runtime arity warning for the dynamic macro should be logged
expect(warnings.some(w => w.includes('Macro "dyn"') && w.includes('unnamed arguments'))).toBeTruthy();
});
});
});
/**
* Evaluates the given input string using the MacroEngine inside the browser
* context, ensuring that the core macros are registered.
*
* @param {import('@playwright/test').Page} page
* @param {string} input
* @returns {Promise<string>}
*/
async function evaluateWithEngine(page, input) {
const result = await page.evaluate(async (input) => {
/** @type {import('../../public/scripts/macros/engine/MacroEngine.js')} */
const { MacroEngine } = await import('./scripts/macros/engine/MacroEngine.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const rawEnv = {
content: input,
name1Override: 'User',
name2Override: 'Character',
};
const env = MacroEnvBuilder.buildFromRawEnv(rawEnv);
const output = await MacroEngine.evaluate(input, env);
return output;
}, input);
return result;
}
/**
* Evaluates the given input string while capturing whether any macro-related
* warnings or errors were logged to the browser console.
*
* This is useful for tests that want to assert both the resolved output and
* whether the lexer/parser/engine reported issues (e.g. unterminated macros).
*
* @param {import('@playwright/test').Page} page
* @param {string} input
* @returns {Promise<{ output: string, hasMacroWarnings: boolean, hasMacroErrors: boolean }>}
*/
async function evaluateWithEngineAndCaptureMacroLogs(page, input) {
/** @type {boolean} */
let hasMacroWarnings = false;
/** @type {boolean} */
let hasMacroErrors = false;
/** @param {import('playwright').ConsoleMessage} msg */
const handler = (msg) => {
const text = msg.text();
if (text.includes('[Macro] Warning:')) {
hasMacroWarnings = true;
}
if (text.includes('[Macro] Error:')) {
hasMacroErrors = true;
}
};
page.on('console', handler);
try {
const output = await evaluateWithEngine(page, input);
return { output, hasMacroWarnings, hasMacroErrors };
} finally {
page.off('console', handler);
}
}
+312
View File
@@ -0,0 +1,312 @@
import { test, expect } from '@playwright/test';
import { testSetup } from './frontent-test-utils.js';
test.describe('MacroEnvBuilder', () => {
test.beforeEach(testSetup.awaitST);
test('builds names from overrides without relying on globals', async ({ page }) => {
const result = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: 'ignored',
name1Override: 'UserOverride',
name2Override: 'CharOverride',
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return {
user: env.names?.user,
char: env.names?.char,
};
});
expect(result).toEqual({
user: 'UserOverride',
char: 'CharOverride',
});
});
test('falls back to global name1/name2 when overrides are not provided', async ({ page }) => {
const result = await page.evaluate(async () => {
const script = await import('./script.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return {
globalUser: script.name1,
globalChar: script.name2,
envUser: env.names?.user,
envChar: env.names?.char,
};
});
expect(result.envUser).toBe(result.globalUser);
expect(result.envChar).toBe(result.globalChar);
});
test('does not populate character fields when replaceCharacterCard is false', async ({ page }) => {
const keys = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
replaceCharacterCard: false,
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return Object.keys(env.character || {});
});
expect(keys).toEqual([]);
});
test('populates character fields when replaceCharacterCard is true', async ({ page }) => {
const keys = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
replaceCharacterCard: true,
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return Object.keys(env.character || {});
});
// We do not assert on concrete values, only that the known keys exist
expect(keys).toEqual(expect.arrayContaining([
'charPrompt',
'charInstruction',
'description',
'personality',
'scenario',
'persona',
'mesExamplesRaw',
'version',
'charDepthPrompt',
'creatorNotes',
]));
});
test('wraps original string into a one-shot helper function', async ({ page }) => {
const result = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
original: 'ORIGINAL_VALUE',
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
const hasFn = typeof env.functions?.original === 'function';
const first = hasFn ? env.functions.original() : null;
const second = hasFn ? env.functions.original() : null;
return { hasFn, first, second };
});
expect(result).toEqual({
hasFn: true,
first: 'ORIGINAL_VALUE',
second: '',
});
});
test('does not expose original helper when original is not a string', async ({ page }) => {
const hasFn = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
original: undefined,
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return typeof env.functions?.original === 'function';
});
expect(hasFn).toBe(false);
});
test('uses groupOverride string for all group-related name fields', async ({ page }) => {
const result = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
groupOverride: 'Group One, Group Two',
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return {
group: env.names?.group,
groupNotMuted: env.names?.groupNotMuted,
notChar: env.names?.notChar,
};
});
expect(result).toEqual({
group: 'Group One, Group Two',
groupNotMuted: 'Group One, Group Two',
notChar: 'Group One, Group Two',
});
});
test('uses solo-chat semantics when no group is selected', async ({ page }) => {
const result = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
const groupChats = await import('./scripts/group-chats.js');
// Ensure we are in a solo-chat like state for this test
if (typeof groupChats.resetSelectedGroup === 'function') {
groupChats.resetSelectedGroup();
}
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
name1Override: 'UserSolo',
name2Override: 'CharSolo',
groupOverride: undefined,
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return {
group: env.names?.group,
groupNotMuted: env.names?.groupNotMuted,
notChar: env.names?.notChar,
};
});
expect(result).toEqual({
group: 'CharSolo',
groupNotMuted: 'CharSolo',
notChar: 'UserSolo',
});
});
test('merges dynamicMacros properties into env.dynamicMacros', async ({ page }) => {
const dynamicMacros = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
dynamicMacros: {
simple: 'value',
number: 42,
nested: { foo: 'bar' },
},
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return env.dynamicMacros;
});
expect(dynamicMacros.simple).toBe('value');
expect(dynamicMacros.number).toBe(42);
expect(dynamicMacros.nested).toEqual({ foo: 'bar' });
});
test('sets system.model field from getGeneratingModel helper', async ({ page }) => {
const model = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return env.system?.model;
});
expect(typeof model === 'string' || model === undefined).toBe(true);
});
test('applies providers in the expected order buckets', async ({ page }) => {
const order = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder, env_provider_order } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
MacroEnvBuilder.registerProvider((env) => {
env.extra.order = [...(env.extra.order || []), 'EARLY'];
}, env_provider_order.EARLY);
MacroEnvBuilder.registerProvider((env) => {
env.extra.order = [...(env.extra.order || []), 'LATE'];
}, env_provider_order.LATE);
MacroEnvBuilder.registerProvider((env) => {
env.extra.order = [...(env.extra.order || []), 'NORMAL'];
}, env_provider_order.NORMAL);
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return env.extra.order;
});
// We only guarantee relative ordering between the buckets we added,
// not that there are no other entries from other providers.
const earlyIndex = order.indexOf('EARLY');
const normalIndex = order.indexOf('NORMAL');
const lateIndex = order.indexOf('LATE');
expect(earlyIndex).toBeGreaterThanOrEqual(0);
expect(normalIndex).toBeGreaterThan(earlyIndex);
expect(lateIndex).toBeGreaterThan(normalIndex);
});
test('ignores provider errors without breaking env construction', async ({ page }) => {
const result = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js')} */
const { MacroEnvBuilder, env_provider_order } = await import('./scripts/macros/engine/MacroEnvBuilder.js');
MacroEnvBuilder.registerProvider(() => {
throw new Error('intentional test error');
}, env_provider_order.NORMAL);
/** @type {import('../../public/scripts/macros/engine/MacroEnvBuilder.js').MacroEnvRawContext} */
const ctx = {
content: '',
name1Override: 'User',
dynamicMacros: { marker: 'value' },
};
const env = MacroEnvBuilder.buildFromRawEnv(ctx);
return {
namesUser: env.names?.user,
hasDynamicMacro: env.dynamicMacros?.marker === 'value',
};
});
expect(result.hasDynamicMacro).toBe(true);
expect(result.namesUser).toBe('User');
});
});
File diff suppressed because it is too large Load Diff
+678
View File
@@ -0,0 +1,678 @@
import { test, expect } from '@playwright/test';
import { testSetup } from './frontent-test-utils.js';
/** @typedef {import('chevrotain').CstNode} CstNode */
/** @typedef {import('chevrotain').IRecognitionException} IRecognitionException */
/** @typedef {{[tokenName: string]: (string|string[]|TestableCstNode|TestableCstNode[])}} TestableCstNode */
/** @typedef {{name: string, message: string}} TestableRecognitionException */
const DEFAULT_FLATTEN_KEYS = [
'arguments.Args.DoubleColon',
];
const DEFAULT_IGNORE_KEYS = [
];
test.describe('MacroParser', () => {
// Currently this test suits runs without ST context. Enable, if ever needed
test.beforeEach(testSetup.goST);
test.describe('General Macro', () => {
// {{user}}
test('should parse a simple macro', async ({ page }) => {
const input = '{{user}}';
const macroCst = await runParser(page, input);
const expectedCst = {
'Macro.Start': '{{',
'Macro.identifier': 'user',
'Macro.End': '}}',
};
expect(macroCst).toEqual(expectedCst);
});
// {{ user }}
test('should generally handle whitespaces', async ({ page }) => {
const input = '{{ user }}';
const macroCst = await runParser(page, input);
const expectedCst = {
'Macro.Start': '{{',
'Macro.identifier': 'user',
'Macro.End': '}}',
};
expect(macroCst).toEqual(expectedCst);
});
test.describe('Error Cases (General Macro)', () => {
// {{}}
test('[Error] should throw an error for empty macro', async ({ page }) => {
const input = '{{}}';
const { macroCst, errors } = await runParserAndGetErrors(page, input);
const expectedErrors = [
{ name: 'NoViableAltException' },
];
const expectedMessage = /Expecting: one of these possible Token sequences:(.*?)\[Macro\.Identifier\](.*?)but found: '}}'/gs;
expect(macroCst).toBeUndefined();
expect(errors).toMatchObject(expectedErrors);
expect(errors[0].message).toMatch(expectedMessage);
});
// {{§!#&blah}}
test('[Error] should throw an error for invalid identifier', async ({ page }) => {
const input = '{{§!#&blah}}';
const { macroCst, errors } = await runParserAndGetErrors(page, input);
const expectedErrors = [
{ name: 'NoViableAltException' },
];
const expectedMessage = /Expecting: one of these possible Token sequences:(.*?)\[Macro\.Identifier\](.*?)but found: '!'/gs;
expect(macroCst).toBeUndefined();
expect(errors).toMatchObject(expectedErrors);
expect(errors[0].message).toMatch(expectedMessage);
});
// {{user
test('[Error] should throw an error for incomplete macro', async ({ page }) => {
const input = '{{user';
const { macroCst, errors } = await runParserAndGetErrors(page, input);
const expectedErrors = [
{ name: 'MismatchedTokenException', message: 'Expecting token of type --> Macro.End <-- but found --> \'\' <--' },
];
expect(macroCst).toBeUndefined();
expect(errors).toEqual(expectedErrors);
});
// something{{user}}
test('[Error] for testing purposes, macros need to start at the beginning of the string', async ({ page }) => {
const input = 'something{{user}}';
const { macroCst, errors } = await runParserAndGetErrors(page, input);
const expectedErrors = [
{ name: 'MismatchedTokenException', message: 'Expecting token of type --> Macro.Start <-- but found --> \'something\' <--' },
];
expect(macroCst).toBeUndefined();
expect(errors).toEqual(expectedErrors);
});
});
});
test.describe('Arguments Handling', () => {
// {{getvar::myvar}}
test('should parse macros with double-colon argument', async ({ page }) => {
const input = '{{getvar::myvar}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'getvar',
'arguments': {
'separator': '::',
'argument': 'myvar',
},
'Macro.End': '}}',
});
});
// {{roll:3d20}}
test('should parse macros with single colon argument', async ({ page }) => {
const input = '{{roll:3d20}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'roll',
'arguments': {
'separator': ':',
'argument': '3d20',
},
'Macro.End': '}}',
});
});
// {{setvar::myvar::value}}
test('should parse macros with multiple double-colon arguments', async ({ page }) => {
const input = '{{setvar::myvar::value}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
ignoreKeys: ['arguments.Args.DoubleColon'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'setvar',
'arguments': {
'separator': '::',
'argument': ['myvar', 'value'],
},
'Macro.End': '}}',
});
});
// {{something:: spaced }}
test('should strip spaces around arguments', async ({ page }) => {
const input = '{{something:: spaced }}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
ignoreKeys: ['arguments.separator', 'arguments.Args.DoubleColon'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'something',
'arguments': { 'argument': 'spaced' },
'Macro.End': '}}',
});
});
// {{something::with:single:colons}}
test('should treat single colons as part of the argument with double-colon separator', async ({ page }) => {
const input = '{{something::with:single:colons}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
ignoreKeys: ['arguments.Args.DoubleColon'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'something',
'arguments': {
'separator': '::',
'argument': 'with:single:colons',
},
'Macro.End': '}}',
});
});
// {{legacy:something:else}}
test('should treat single colons as part of the argument even with colon separator', async ({ page }) => {
const input = '{{legacy:something:else}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
ignoreKeys: ['arguments.separator', 'arguments.Args.Colon'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'legacy',
'arguments': { 'argument': 'something:else' },
'Macro.End': '}}',
});
});
// {{something::}}
test('should parse double-colon with an empty argument value', async ({ page }) => {
const input = '{{something::}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'something',
'arguments': {
'separator': '::',
'argument': '',
},
'Macro.End': '}}',
});
});
});
test.describe('Legacy Macros', () => {
// {{roll 1d5}}
test('should parse legacy roll macro with whitespace separator', async ({ page }) => {
const input = '{{roll 1d5}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'roll',
'arguments': { 'argument': '1d5' },
'Macro.End': '}}',
});
});
// {{roll:2d20}}
test('should parse legacy roll macro with explicit colon separator', async ({ page }) => {
const input = '{{roll:2d20}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'roll',
'arguments': {
'separator': ':',
'argument': '2d20',
},
'Macro.End': '}}',
});
});
// {{roll 20}}
test('should parse legacy roll macro with numeric argument', async ({ page }) => {
const input = '{{roll 20}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'roll',
'arguments': { 'argument': '20' },
'Macro.End': '}}',
});
});
// {{reverse:something}}
test('should parse reverse legacy macro with colon argument', async ({ page }) => {
const input = '{{reverse:something}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'reverse',
'arguments': {
'separator': ':',
'argument': 'something',
},
'Macro.End': '}}',
});
});
// {{reverse:this contains::double::colons}}
test('should parse legacy single colon argument that allows double colons inside the argument', async ({ page }) => {
const input = '{{reverse:this contains::double::colons}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'reverse',
'arguments': {
'separator': ':',
'argument': 'this contains::double::colons',
},
'Macro.End': '}}',
});
});
// {{//comment-style macro}}
// TODO: Comment like // is not a valid identifier, needs to be an exception (until we maybe add flags)
test('should parse legacy comment macro', async ({ page }) => {
const input = '{{//comment-style macro}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': '//',
'arguments': { 'argument': 'comment-style macro' },
'Macro.End': '}}',
});
});
// {{datetimeformat HH:mm}}
test('should parse legacy datetime format macro', async ({ page }) => {
const input = '{{datetimeformat HH:mm}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'datetimeformat',
'arguments': { 'argument': 'HH:mm' },
'Macro.End': '}}',
});
});
// Note: Legacy time macros like {{time_UTC+2}} are now handled by the MacroEngine
// pre-processing pipeline instead of the parser. See MacroEngine.e2e tests for coverage.
// {{banned "abannedword"}}
test('should parse legacy banned macro with quoted argument', async ({ page }) => {
const input = '{{banned "abannedword"}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'banned',
'arguments': { 'argument': '"abannedword"' },
'Macro.End': '}}',
});
});
// {{banned ""}}
test('should parse legacy macro with empty quoted argument', async ({ page }) => {
const input = '{{banned ""}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'banned',
'arguments': { 'argument': '""' },
'Macro.End': '}}',
});
});
// {{setvar::myvar::}}
test('should allow legacy setvar with empty value argument', async ({ page }) => {
const input = '{{setvar::myvar::}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'setvar',
'arguments': {
'separator': '::',
'argument': ['myvar', ''],
},
'Macro.End': '}}',
});
});
});
test.describe('Comment Macros', () => {
// {{//comment}}
test('should parse comment macro without whitespace', async ({ page }) => {
const input = '{{//comment}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': '//',
'Macro.End': '}}',
'arguments': {
'argument': 'comment',
},
});
});
// {{// comment}}
test('should parse comment macro with whitespace', async ({ page }) => {
const input = '{{// comment}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': '//',
'Macro.End': '}}',
'arguments': {
'argument': 'comment',
},
});
});
// {{//!@#$%^&*()_+}}
test('should parse comment macro with special characters', async ({ page }) => {
const input = '{{//!@#$%^&*()_+}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': '//',
'Macro.End': '}}',
'arguments': {
'argument': '!@#$%^&*()_+',
},
});
});
// {{//!@flags}}
test('should parse comment macro starting with flags', async ({ page }) => {
const input = '{{//!@flags}}';
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': '//',
'Macro.End': '}}',
'arguments': {
'argument': '!@flags',
},
});
});
// {{// This is a multiline comment.
// This is the second line
// }}
test('should parse multiline comments', async ({ page }) => {
const input = `{{// This is a multiline comment.
This is the second line
}}`;
const macroCst = await runParser(page, input, {
flattenKeys: ['arguments.argument'],
});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': '//',
'Macro.End': '}}',
'arguments': {
'argument': 'This is a multiline comment.\nThis is the second line',
},
});
});
});
test.describe('Nested Macros', () => {
// {{outer::word {{inner}}}}
test('should parse nested macros inside arguments', async ({ page }) => {
const input = '{{outer::word {{inner}}}}';
const macroCst = await runParser(page, input, {});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'outer',
'arguments': {
'argument': {
'Identifier': 'word',
'macro': {
'Macro.Start': '{{',
'Macro.identifier': 'inner',
'Macro.End': '}}',
},
},
'separator': '::',
},
'Macro.End': '}}',
});
});
// {{outer::word {{inner1}}{{inner2}}}}
test('should parse two nested macros next to each other inside an argument', async ({ page }) => {
const input = '{{outer::word {{inner1}}{{inner2}}}}';
const macroCst = await runParser(page, input, {});
expect(macroCst).toEqual({
'Macro.Start': '{{',
'Macro.identifier': 'outer',
'arguments': {
'argument': {
'Identifier': 'word',
'macro': [
{
'Macro.Start': '{{',
'Macro.identifier': 'inner1',
'Macro.End': '}}',
},
{
'Macro.Start': '{{',
'Macro.identifier': 'inner2',
'Macro.End': '}}',
},
],
},
'separator': '::',
},
'Macro.End': '}}',
});
});
test.describe('Error Cases (Nested Macros)', () => {
// {{{{macroindentifier}}::value}}
test('[Error] should throw when there is a nested macro instead of an identifier', async ({ page }) => {
const input = '{{{{macroindentifier}}::value}}';
const { macroCst, errors } = await runParserAndGetErrors(page, input);
expect(macroCst).toBeUndefined();
expect(errors).toHaveLength(1); // error doesn't really matter. Just don't parse it pls.
});
// {{inside{{macro}}me}}
test('[Error] should throw when there is a macro inside an identifier', async ({ page }) => {
const input = '{{inside{{macro}}me}}';
const { macroCst, errors } = await runParserAndGetErrors(page, input);
expect(macroCst).toBeUndefined();
expect(errors).toHaveLength(1); // error doesn't really matter. Just don't parse it pls.
});
});
});
});
/**
* Runs the input through the MacroParser and returns the result.
*
* @param {import('@playwright/test').Page} page - The Playwright page object.
* @param {string} input - The input string to be parsed.
* @param {Object} [options={}] Optional arguments
* @param {string[]} [options.flattenKeys=[]] Optional array of dot-separated keys to flatten
* @param {string[]} [options.ignoreKeys=[]] Optional array of dot-separated keys to ignore
* @returns {Promise<TestableCstNode>} A promise that resolves to the result of the MacroParser.
*/
async function runParser(page, input, options = {}) {
const { cst, errors } = await runParserAndGetErrors(page, input, options);
// Make sure that parser errors get correctly marked as errors during testing, even if the resulting structure might work.
// If we don't test for errors, the test should fail.
if (errors.length > 0) {
throw new Error('Parser errors found\n' + errors.map(x => x.message).join('\n'));
}
return cst;
}
/**
* Runs the input through the MacroParser and returns the syntax tree result and any parser errors.
*
* Use `runParser` if you don't want to explicitly test against parser errors.
*
* @param {import('@playwright/test').Page} page - The Playwright page object.
* @param {string} input - The input string to be parsed.
* @param {Object} [options={}] Optional arguments
* @param {string[]} [options.flattenKeys=[]] Optional array of dot-separated keys to flatten
* @param {string[]} [options.ignoreKeys=[]] Optional array of dot-separated keys to ignore
* @returns {Promise<{cst: TestableCstNode, errors: TestableRecognitionException[]}>} A promise that resolves to the result of the MacroParser and error list.
*/
async function runParserAndGetErrors(page, input, options = {}) {
const params = { input, options };
const { result } = await page.evaluate(async ({ input, options }) => {
/** @type {import('../../public/scripts/macros/engine/MacroParser.js')} */
const { MacroParser } = await import('./scripts/macros/engine/MacroParser.js');
const result = MacroParser.test(input);
return { result };
}, params);
return { cst: simplifyCstNode(result.cst, input, options), errors: simplifyErrors(result.errors) };
}
/**
* Simplify the parser syntax tree result into an easily testable format.
*
* @param {CstNode} result The result from the parser
* @param {Object} [options={}] Optional arguments
* @param {string[]} [options.flattenKeys=[]] Optional array of dot-separated keys to flatten
* @param {string[]} [options.ignoreKeys=[]] Optional array of dot-separated keys to ignore
* @returns {TestableCstNode} The testable syntax tree
*/
function simplifyCstNode(cst, input, { flattenKeys = [], ignoreKeys = [], ignoreDefaultFlattenKeys = false, ignoreDefaultIgnoreKeys = false } = {}) {
if (!ignoreDefaultFlattenKeys) flattenKeys = [...flattenKeys, ...DEFAULT_FLATTEN_KEYS];
if (!ignoreDefaultIgnoreKeys) ignoreKeys = [...ignoreKeys, ...DEFAULT_IGNORE_KEYS];
/** @returns {TestableCstNode} @param {CstNode} node @param {string[]} path */
function simplifyNode(node, path = []) {
if (!node) return node;
if (Array.isArray(node)) {
// Single-element arrays are converted to a single string
if (node.length === 1) {
return node[0].image || simplifyNode(node[0], path.concat('[]'));
}
// For multiple elements, return an array of simplified nodes
return node.map(child => simplifyNode(child, path.concat('[]')));
}
if (node.children) {
const simplifiedChildren = {};
for (const key in node.children) {
function simplifyChildNode(childNode, path) {
if (Array.isArray(childNode)) {
// Single-element arrays are converted to a single string
if (childNode.length === 1) {
return simplifyChildNode(childNode[0], path.concat('[]'));
}
return childNode.map(child => simplifyChildNode(child, path.concat('[]')));
}
const flattenKey = path.filter(x => x !== '[]').join('.');
if (ignoreKeys.includes(flattenKey)) {
return null;
} else if (flattenKeys.includes(flattenKey)) {
if (!childNode.location) return null;
const startOffset = childNode.location.startOffset;
const endOffset = childNode.location.endOffset;
return input.slice(startOffset, endOffset + 1);
} else {
return simplifyNode(childNode, path);
}
}
const simplifiedValue = simplifyChildNode(node.children[key], path.concat(key));
if (simplifiedValue !== null) simplifiedChildren[key] = simplifiedValue;
}
if (Object.values(simplifiedChildren).length === 0) return null;
return simplifiedChildren;
}
return node.image;
}
return simplifyNode(cst);
}
/**
* Simplifies a recognition exceptions into an easily testable format.
*
* @param {IRecognitionException[]} errors - The error list containing exceptions to be simplified.
* @return {TestableRecognitionException[]} - The simplified error list
*/
function simplifyErrors(errors) {
return errors.map(exception => ({
name: exception.name,
message: exception.message,
}));
}
+153
View File
@@ -0,0 +1,153 @@
import { test, expect } from '@playwright/test';
import { testSetup } from './frontent-test-utils.js';
test.describe('MacroRegistry', () => {
// Currently this test suits runs without ST context. Enable, if ever needed
test.beforeEach(testSetup.awaitST);
test.describe('valid', () => {
test('should register a macro with valid options', async ({ page }) => {
const result = await page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
MacroRegistry.unregisterMacro('test-valid');
MacroRegistry.registerMacro('test-valid', {
unnamedArgs: 2,
list: { min: 1, max: 3 },
strictArgs: false,
description: 'Test macro for validation.',
handler: ({ args }) => args.join(','),
});
const def = MacroRegistry.getMacro('test-valid');
return {
name: def?.name,
minArgs: def?.minArgs,
maxArgs: def?.maxArgs,
list: def?.list,
strictArgs: def?.strictArgs,
description: def?.description,
};
});
expect(result).toEqual({
name: 'test-valid',
minArgs: 2,
maxArgs: 2,
list: { min: 1, max: 3 },
strictArgs: false,
description: 'Test macro for validation.',
});
});
});
test.describe('reject', () => {
test('should reject invalid macro name', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// Empty name
MacroRegistry.registerMacro(' ', {
handler: () => '',
});
})).rejects.toThrow(/Macro name must be a non-empty string/);
});
test('should reject invalid options object', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// Options must be object
// @ts-expect-error intentionally wrong
MacroRegistry.registerMacro('invalid-options', null);
})).rejects.toThrow(/options must be a non-null object/);
});
test('should reject invalid handler', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// Handler must be function
// @ts-expect-error intentionally wrong
MacroRegistry.registerMacro('no-handler', { handler: null });
})).rejects.toThrow(/options\.handler must be a function/);
});
test('should reject invalid unnamedArgs', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// unnamedArgs must be non-negative integer
MacroRegistry.registerMacro('bad-required', {
// @ts-expect-error intentionally wrong
unnamedArgs: -1,
handler: () => '',
});
})).rejects.toThrow(/options\.unnamedArgs must be a non-negative integer/);
});
test('should reject invalid strictArgs', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// strictArgs must be boolean
MacroRegistry.registerMacro('bad-strict', {
// @ts-expect-error intentionally wrong
strictArgs: 'yes',
handler: () => '',
});
})).rejects.toThrow(/options\.strictArgs must be a boolean/);
});
test('should reject invalid list configuration', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// list must be boolean or object
MacroRegistry.registerMacro('bad-list-type', {
// @ts-expect-error intentionally wrong
list: 'invalid',
handler: () => '',
});
})).rejects.toThrow(/options\.list must be a boolean or an object/);
});
test('should reject invalid list.min', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// list.min must be non-negative
MacroRegistry.registerMacro('bad-list-min', {
list: { min: -1 },
handler: () => '',
});
})).rejects.toThrow(/options\.list\.min must be a non-negative integer/);
});
test('should reject invalid list.max', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// list.max must be >= min
MacroRegistry.registerMacro('bad-list-max', {
list: { min: 2, max: 1 },
handler: () => '',
});
})).rejects.toThrow(/options\.list\.max must be greater than or equal to options\.list\.min/);
});
test('should reject invalid description', async ({ page }) => {
await expect(page.evaluate(async () => {
/** @type {import('../../public/scripts/macros/engine/MacroRegistry.js')} */
const { MacroRegistry } = await import('./scripts/macros/engine/MacroRegistry.js');
// description must be string
MacroRegistry.registerMacro('bad-desc', {
// @ts-expect-error intentionally wrong
description: 123,
handler: () => '',
});
})).rejects.toThrow(/options\.description must be a string/);
});
});
});
+50
View File
@@ -0,0 +1,50 @@
import { test, expect } from '@playwright/test';
import { testSetup } from './frontent-test-utils.js';
// Tests for the deprecated MacrosParser shim to ensure it continues to work
// both with the legacy regex macro system (feature flag disabled) and with
// the new macro engine (feature flag enabled).
test.describe('MacrosParser (legacy shim)', () => {
test.beforeEach(testSetup.awaitST);
test('should resolve macros via legacy evaluateMacros when experimental engine is disabled', async ({ page }) => {
const output = await page.evaluate(async () => {
const { MacrosParser, evaluateMacros } = await import('./scripts/macros.js');
const { power_user } = await import('./scripts/power-user.js');
power_user.experimental_macro_engine = false;
MacrosParser.registerMacro('legacyParserTest', 'LEGACY_OK', 'Legacy parser test');
const env = {};
const result = evaluateMacros('Value: {{legacyParserTest}}.', env, (x) => x);
MacrosParser.unregisterMacro('legacyParserTest');
return result;
});
expect(output).toBe('Value: LEGACY_OK.');
});
test('should resolve macros via new engine when experimental engine is enabled', async ({ page }) => {
const output = await page.evaluate(async () => {
const { MacrosParser } = await import('./scripts/macros.js');
const { substituteParams } = await import('./script.js');
const { power_user } = await import('./scripts/power-user.js');
power_user.experimental_macro_engine = true;
MacrosParser.registerMacro('engineParserTest', 'ENGINE_OK', 'Engine parser test');
const result = substituteParams('Value: {{engineParserTest}}.', {});
MacrosParser.unregisterMacro('engineParserTest');
return result;
});
expect(output).toBe('Value: ENGINE_OK.');
});
});