perf(test): skip tts provider lookup without directives

This commit is contained in:
Peter Steinberger
2026-04-20 14:51:29 +01:00
parent 4da0a99a9e
commit 17c77f1307
2 changed files with 40 additions and 6 deletions

View File

@@ -56,6 +56,28 @@ const fullPolicy: SpeechModelOverridePolicy = {
};
describe("parseTtsDirectives provider-aware routing", () => {
it("does not resolve providers when text has no directives", () => {
const failProvider = {
get id() {
throw new Error("provider should not be read without directives");
},
get autoSelectOrder() {
throw new Error("provider order should not be read without directives");
},
} as unknown as SpeechProviderPlugin;
const result = parseTtsDirectives("hello without TTS markup", fullPolicy, {
providers: [failProvider, failProvider],
});
expect(result).toEqual({
cleanedText: "hello without TTS markup",
overrides: {},
warnings: [],
hasDirective: false,
});
});
it("routes generic speed to the explicitly declared provider", () => {
const result = parseTtsDirectives(
"hello [[tts:provider=minimax speed=1.2]] world",

View File

@@ -62,7 +62,15 @@ export function parseTtsDirectives(
return { cleanedText: text, overrides: {}, warnings: [], hasDirective: false };
}
const providers = resolveDirectiveProviders(options);
if (!/\[\[tts:/iu.test(text)) {
return { cleanedText: text, overrides: {}, warnings: [], hasDirective: false };
}
let providers: SpeechProviderPlugin[] | undefined;
const getProviders = () => {
providers ??= resolveDirectiveProviders(options);
return providers;
};
const overrides: TtsDirectiveOverrides = {};
const warnings: string[] = [];
let cleanedText = text;
@@ -107,10 +115,14 @@ export function parseTtsDirectives(
}
}
const orderedProviders = prioritizeProvider(
providers,
declaredProviderId ?? normalizeLowercaseStringOrEmpty(options?.preferredProviderId),
);
let orderedProviders: SpeechProviderPlugin[] | undefined;
const getOrderedProviders = () => {
orderedProviders ??= prioritizeProvider(
getProviders(),
declaredProviderId ?? normalizeLowercaseStringOrEmpty(options?.preferredProviderId),
);
return orderedProviders;
};
for (const token of tokens) {
const eqIndex = token.indexOf("=");
@@ -127,7 +139,7 @@ export function parseTtsDirectives(
continue;
}
for (const provider of orderedProviders) {
for (const provider of getOrderedProviders()) {
const parsed = provider.parseDirectiveToken?.({
key,
value: rawValue,