Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raidboss/test: improve testing of timeline netregex #5968

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion test/helper/test_timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';

import { assert } from 'chai';

import { keysThatRequireTranslation } from '../../resources/netregexes';
import Regexes from '../../resources/regexes';
import { translateWithReplacements } from '../../resources/translations';
import { LooseTriggerSet } from '../../types/trigger';
Expand Down Expand Up @@ -75,10 +76,30 @@ const getTestCases = (
for (const [key, replaceText] of Object.entries(trans.replaceText ?? {}))
textMap.set(Regexes.parse(key), replaceText);

// Add all original regexes to the set of things to do replacement on
// and add all translatable parameters as well.
const syncStrings: Set<string> = new Set<string>();
for (const sync of timeline.syncStarts) {
if (typeof sync.origInput === 'string') {
syncStrings.add(sync.origInput);
continue;
}
for (const [key, value] of Object.entries(sync.origInput)) {
if (!keysThatRequireTranslation.includes(key))
continue;
if (typeof value === 'object') {
for (const innerValue of value)
syncStrings.add(innerValue);
} else {
syncStrings.add(value);
}
}
}

const testCases: TestCase[] = [
{
type: 'replaceSync',
items: new Set(timeline.syncStarts.map((x) => x.regex.source)),
items: syncStrings,
replace: new Map(syncMap),
replaceWithoutCommon: new Map(syncMap),
},
Expand Down Expand Up @@ -314,6 +335,8 @@ const testTimelineFiles = (timelineFiles: string[]): void => {
it('should have proper sealed sync', () => {
for (const sync of timeline.syncStarts) {
const regex = sync.regex.source;
if (sync.regexType === 'net')
continue;
if (regex.includes('is no longer sealed')) {
assert.isArray(
/00:0839::\.\*is no longer sealed/.exec(regex),
Expand Down
27 changes: 23 additions & 4 deletions ui/raidboss/timeline_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { buildNetRegexForTrigger } from '../../resources/netregexes';
import { UnreachableCode } from '../../resources/not_reached';
import Regexes from '../../resources/regexes';
import {
AnonNetRegexParams,
translateRegex,
translateRegexBuildParamAnon,
translateText,
Expand All @@ -33,6 +32,18 @@ const isStringOrStringArray = (value: unknown): value is string | string[] => {
return typeof value === 'string';
};

export type TimelineNetParams = { [key: string]: string | string[] };
const isTimelineNetParams = (value: unknown): value is TimelineNetParams => {
if (typeof value !== 'object' || Array.isArray(value))
return false;
const obj = value as { [key: string]: unknown };
for (const innerValue of Object.values(obj)) {
if (!isStringOrStringArray(innerValue))
return false;
}
return true;
};

const isValidNetParams = <T extends LogDefinitionTypes>(
type: T,
params: Record<string, unknown>,
Expand All @@ -44,6 +55,9 @@ const isValidNetParams = <T extends LogDefinitionTypes>(
// Make sure our value is either a string/int or an array of strings/ints
if (!isStringOrStringArray(params[key]))
return false;
// These should never be specified on a timeline net regex.
if (key === 'capture' || key === 'timestamp')
return false;
}
return true;
};
Expand Down Expand Up @@ -87,7 +101,7 @@ export type Error = {

export type Sync = {
id: number;
origInput: string | AnonNetRegexParams;
origInput: string | TimelineNetParams;
regexType: 'parsed' | 'net';
regex: RegExp;
start: number;
Expand Down Expand Up @@ -512,10 +526,15 @@ export class TimelineParser {
this.options.ParserLanguage,
this.replacements,
).params;

// The original params should be TimelineNetParams, thus so should the output.
if (!isTimelineNetParams(translatedParams))
throw new UnreachableCode();

return this.buildRegexSync(
uniqueid,
'net',
params,
translatedParams,
Regexes.parse(buildNetRegexForTrigger(netRegexType, translatedParams)),
syncCommand.args,
seconds,
Expand Down Expand Up @@ -555,7 +574,7 @@ export class TimelineParser {
private buildRegexSync(
uniqueid: number,
regexType: 'parsed' | 'net',
origInput: string | AnonNetRegexParams,
origInput: string | TimelineNetParams,
parsedRegex: RegExp,
args: string | undefined,
seconds: number,
Expand Down