You need to fix a small utility function that prepares the cache check result for the status panel.
Reproduction environment: TypeScript 5.8, strict=true, Node.js 22, ES modules. Build and run are done via npm test without additional packages.
The contract of buildCacheBadge(input, cache):
input can be string | null | undefined.{ key, label, cached }.key must be the normalized query string.label must be the original value without trimming.cached must reflect the presence of an entry in cache by the normalized key.null.Simulate the behavior step by step:
null.In the current implementation there are exactly two independent problems: one is related to input string validation, and the other is that the returned object shares internal state with the cache. Fix both.
Assumption: the cache is a regular Map<string, { count: number }> and its values must not be mutated through the function result.
TypeScript 5.8 with strict=true, Node.js 22, ES modules. No extra dependencies; compile and run with npm test.
Use line comments to explain the problem, its impact, the fix and how to test it.