feat(redis): auto-format detected JSON values

This commit is contained in:
弈秋 authored and GitHub committed 2026-10-03 00:21:59 +08:00
1 parent 06918c06c8
commit 83651c46d6
15 files changed
+343 -13

No files matched your search

@@ -0,0 +1,124 @@
import { readFileSync } from "node:fs";
import { parse } from "vue/compiler-sfc";
import ts from "typescript";
import { describe, expect, it } from "vitest";
const viewerSource = readFileSync(new URL("../../../components/redis/RedisValueViewer.vue", import.meta.url), "utf8");
const parsedViewer = parse(viewerSource, { filename: "RedisValueViewer.vue" });
/**
* Issue #10922 wiring: detected JSON containers open pretty-printed, detected
* structured codecs open decoded, and both surfaces carry a representation
* badge — unless the user pinned an explicit format or codec.
*/
describe("Redis value detection (issue 10922)", () => {
function findFunction(name: string): ts.FunctionDeclaration {
const script = parsedViewer.descriptor.scriptSetup;
expect(script).toBeDefined();
const source = ts.createSourceFile("RedisValueViewer.vue.ts", script!.content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
const declaration = source.statements.find((statement): statement is ts.FunctionDeclaration => ts.isFunctionDeclaration(statement) && statement.name?.text === name);
expect(declaration).toBeDefined();
return declaration!;
}
function callsIn(node: ts.Node): ts.CallExpression[] {
const calls: ts.CallExpression[] = [];
const visit = (child: ts.Node) => {
if (ts.isCallExpression(child)) calls.push(child);
ts.forEachChild(child, visit);
};
visit(node);
return calls;
}
function calledName(call: ts.CallExpression): string | undefined {
if (ts.isIdentifier(call.expression)) return call.expression.text;
if (ts.isPropertyAccessExpression(call.expression)) return `${call.expression.expression.getText()}.${call.expression.name.text}`;
return undefined;
}
function assignmentsIn(node: ts.Node): ts.BinaryExpression[] {
const assignments: ts.BinaryExpression[] = [];
const visit = (child: ts.Node) => {
if (ts.isBinaryExpression(child) && child.operatorToken.kind === ts.SyntaxKind.EqualsToken) assignments.push(child);
ts.forEachChild(child, visit);
};
visit(node);
return assignments;
}
function findVariableInitializer(name: string): ts.Expression {
const script = parsedViewer.descriptor.scriptSetup;
expect(script).toBeDefined();
const source = ts.createSourceFile("RedisValueViewer.vue.ts", script!.content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
for (const statement of source.statements) {
if (!ts.isVariableStatement(statement)) continue;
const declaration = statement.declarationList.declarations.find((candidate) => ts.isIdentifier(candidate.name) && candidate.name.text === name);
if (declaration?.initializer) return declaration.initializer;
}
throw new Error(`Expected ${name} to have an initializer`);
}
it("opens detected JSON containers pretty-printed and detected codecs decoded on load", () => {
const load = findFunction("load");
const formatAssignments = assignmentsIn(load).filter((assignment) => assignment.left.getText() === "stringValueView.value");
const codecCalls = callsIn(load).filter((call) => calledName(call) === "applyDetectedStringCodec");
expect(formatAssignments).toHaveLength(1);
expect(formatAssignments[0].right.getText()).toBe("autoRedisValueFormat(detail, readStoredRedisValueFormat())");
expect(codecCalls.map((call) => call.arguments.map((argument) => argument.getText()))).toContainEqual(["detail"]);
});
it("applies the same detection to selected member details", () => {
const selectMember = findFunction("selectMember");
const formatAssignments = assignmentsIn(selectMember).filter((assignment) => assignment.left.getText() === "memberValueView.value");
const codecCalls = callsIn(selectMember).filter((call) => calledName(call) === "applyDetectedMemberCodec");
expect(formatAssignments).toHaveLength(1);
expect(formatAssignments[0].right.getText()).toBe("autoRedisValueFormat(detail, readStoredRedisValueFormat())");
expect(codecCalls.map((call) => call.arguments.map((argument) => argument.getText()))).toContainEqual(["detail"]);
});
it("detection yields to a pinned codec and clears itself for undetected payloads", () => {
const applyString = findFunction("applyDetectedStringCodec").getText();
const applyMember = findFunction("applyDetectedMemberCodec").getText();
for (const text of [applyString, applyMember]) {
expect(text).toContain("readStoredRedisValueCodec() != null");
expect(text).toContain('detectedRedisStructuredCodec(detail) ?? "none"');
}
expect(applyString).toContain("isStringValueTruncated.value");
});
it("treats a missing stored preference as the auto-detection signal", () => {
const readFormat = findFunction("readStoredRedisValueFormat").getText();
const readCodec = findFunction("readStoredRedisValueCodec").getText();
// Legacy stored values keep their mapping; anything unpinned returns null
// (auto), never the old "utf8"/"none" defaults.
expect(readFormat).toContain('if (stored === "raw") return "utf8";');
expect(readFormat).toContain(": null;");
expect(readFormat).not.toContain(': "utf8";');
expect(readCodec).toContain("isRedisValueCodec(stored) ? stored : null");
const stringViewInit = findVariableInitializer("stringValueView").getText();
const stringCodecInit = findVariableInitializer("stringValueCodec").getText();
expect(stringViewInit).toContain('readStoredRedisValueFormat() ?? "utf8"');
expect(stringCodecInit).toContain('readStoredRedisValueCodec() ?? "none"');
});
it("labels the detected representation next to the value on both surfaces", () => {
const template = parsedViewer.descriptor.template;
expect(parsedViewer.errors).toEqual([]);
expect(template).toBeDefined();
expect(viewerSource).toContain('v-if="stringValueDetectionLabel"');
expect(viewerSource).toContain('v-if="memberDetectionLabel"');
expect(viewerSource.match(/t\('redis\.detectedBadgeTitle'\)/g)).toHaveLength(4);
expect(findFunction("redisDetectionBadgeLabel").getText()).toContain('t("redis.detectedJavaSerialized")');
expect(findFunction("redisDetectionBadgeLabel").getText()).toContain("isRedisJsonContainerValue(detail.json?.value)");
});
});
@@ -1,10 +1,13 @@
import { describe, expect, it } from "vitest";
import {
autoRedisValueFormat,
canRenderRedisValueFormat,
detectedRedisStructuredCodec,
formatRedisMemberDetail,
formatRedisStringValue,
getRedisMemberSelectionKey,
isRedisJsonContainerValue,
jsonToXmlText,
jsonToYamlText,
normalizeRedisJsonDraft,
@@ -276,6 +279,69 @@ describe("redisValuePresentation", () => {
expect(canRenderRedisValueFormat(formatRedisMemberDetail("plain-text"), "utf8")).toBe(true);
});
it("auto-opens JSON containers pretty-printed when no format was pinned", () => {
const detail = formatRedisMemberDetail('{"role":"reader"}', { allowJsonText: true });
expect(autoRedisValueFormat(detail, null)).toBe("json");
});
it("keeps scalar JSON and plain text on the raw utf8 view", () => {
expect(autoRedisValueFormat(formatRedisMemberDetail("123", { allowJsonText: true }), null)).toBe("utf8");
expect(autoRedisValueFormat(formatRedisMemberDetail("plain-text", { allowJsonText: true }), null)).toBe("utf8");
});
it("lets a pinned format preference win over JSON detection", () => {
const detail = formatRedisMemberDetail('{"role":"reader"}', { allowJsonText: true });
expect(autoRedisValueFormat(detail, "utf8")).toBe("utf8");
expect(autoRedisValueFormat(detail, "json")).toBe("json");
});
it("keeps non-reusable view preferences falling back to the value default", () => {
const detail = formatRedisMemberDetail('{"role":"reader"}', { allowJsonText: true });
expect(autoRedisValueFormat(detail, "hex")).toBe("utf8");
});
it("still honors binary inspection views on non-editable blobs", () => {
const detail = formatRedisMemberDetail({
raw_base64: Buffer.from([0x00, 0x01, 0x02]).toString("base64"),
encoding: "binary",
});
expect(autoRedisValueFormat(detail, "hex")).toBe("hex");
expect(autoRedisValueFormat(detail, null)).toBe("hex");
});
it("maps detected structured payloads to their codec", () => {
const java = formatRedisMemberDetail({ raw_base64: "rO0ABXQACHNvbWV0ZXh0", encoding: "binary" });
expect(detectedRedisStructuredCodec(java)).toBe("javaserialize");
const plain = formatRedisMemberDetail("plain-text", { allowJsonText: true });
expect(detectedRedisStructuredCodec(plain)).toBeNull();
});
it("auto-detects msgpack only when the decoded value is a container", () => {
// {"a":1} encoded as msgpack.
const container = formatRedisMemberDetail({
raw_base64: Buffer.from([0x81, 0xa1, 0x61, 0x01]).toString("base64"),
encoding: "binary",
});
expect(detectedRedisStructuredCodec(container)).toBe("msgpack");
// Single ASCII bytes are valid msgpack fixints, so scalar payloads must
// stay undetected: a counter key of "1" must not render as 49.
for (const scalar of ["1", "0", "Y", "é"]) {
const detail = formatRedisMemberDetail(scalar, { allowJsonText: true });
expect(detectedRedisStructuredCodec(detail)).toBeNull();
}
});
it("treats only objects and arrays as JSON containers", () => {
expect(isRedisJsonContainerValue({ a: 1 })).toBe(true);
expect(isRedisJsonContainerValue([1])).toBe(true);
expect(isRedisJsonContainerValue("123")).toBe(false);
expect(isRedisJsonContainerValue(null)).toBe(false);
expect(isRedisJsonContainerValue(undefined)).toBe(false);
});
it("keeps legacy Pickle payloads out of conservative auto-detection", () => {
const detail = formatRedisMemberDetail({
raw_base64: "KGRwMApWc3RhdHVzCnAxClZTVUNDRVNTCnAyCnNWcmVzdWx0CnAzCihscDQKSTEKYVZoZWxsbwpwNQphTmFJMDEKYUkwMAphcy4=",
@@ -461,6 +461,40 @@ export function preferredRedisValueFormat(value: unknown, preferred?: RedisValue
return detail.defaultFormat;
}
/**
* Issue #10922: a string payload that parses into a JSON object or array opens
* pretty-printed instead of raw, matching RedisInsight. `stored` is the
* persisted format preference; null means the user never pinned a format, so
* detection wins. Scalar JSON ("123", "\"text\"") renders identically in both
* views, so it never triggers the switch.
*/
export function autoRedisValueFormat(detail: RedisMemberDetail, stored: RedisValueFormat | null): RedisValueFormat {
if (stored != null && shouldReuseRedisValueFormatPreference(detail, stored)) return stored;
if (stored == null && isRedisJsonContainerValue(detail.json?.value)) return "json";
return detail.defaultFormat;
}
export function isRedisJsonContainerValue(value: unknown): boolean {
return typeof value === "object" && value !== null;
}
/**
* Codec that `formatRedisMemberDetail` already decoded for this payload, or
* null. Java serialization, Pickle, and PHP serialization have deterministic
* magic prefixes; Protobuf is deliberately excluded because without a schema
* its wire format matches too many byte strings. Msgpack has no magic prefix
* -- any single byte is a valid msgpack scalar -- so its detection
* additionally requires the decoded value to be a container, matching the
* JSON auto-format guard above.
*/
export function detectedRedisStructuredCodec(detail: RedisMemberDetail): RedisValueCodec | null {
if (detail.javaSerialized) return "javaserialize";
if (detail.pickle) return "pickle";
if (detail.msgpack && isRedisJsonContainerValue(detail.msgpack.value)) return "msgpack";
if (detail.phpSerialized) return "phpserialize";
return null;
}
export function canRenderRedisValueFormat(detail: RedisMemberDetail, format: RedisValueFormat): boolean {
return !isJsonDerivedView(format) || Boolean(detail.json);
}