fix(etcd): preserve raw key identity in browser actions

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-27 13:18:53 +08:00
1 parent 122853d089
commit 39d0f5b30f
69 files changed
+6577 -742

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -0,0 +1,102 @@
<script setup lang="ts">
import { Compartment, EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { basicSetup } from "codemirror";
import { json } from "@codemirror/lang-json";
import { sql } from "@codemirror/lang-sql";
import { yaml } from "@codemirror/lang-yaml";
import { xml } from "@codemirror/lang-xml";
import { StreamLanguage } from "@codemirror/language";
import { dockerFile } from "@codemirror/legacy-modes/mode/dockerfile";
import { nginx } from "@codemirror/legacy-modes/mode/nginx";
import { properties } from "@codemirror/legacy-modes/mode/properties";
import { shell } from "@codemirror/legacy-modes/mode/shell";
import { onBeforeUnmount, onMounted, ref, watch } from "vue";
import type { KvValueFormat } from "@/lib/kv/kvValueFormat";
const props = withDefaults(
defineProps<{
modelValue: string;
format?: KvValueFormat;
readOnly?: boolean;
}>(),
{
format: "text",
readOnly: false,
},
);
const emit = defineEmits<{ "update:modelValue": [value: string] }>();
const host = ref<HTMLElement>();
const languageCompartment = new Compartment();
const readOnlyCompartment = new Compartment();
let view: EditorView | null = null;
function languageExtension(format: KvValueFormat) {
if (format === "json") return json();
if (format === "yaml" || format === "kubernetes") return yaml();
if (format === "xml") return xml();
if (format === "sql") return sql();
if (format === "properties") return StreamLanguage.define(properties);
if (format === "shell") return StreamLanguage.define(shell);
if (format === "dockerfile") return StreamLanguage.define(dockerFile);
if (format === "nginx") return StreamLanguage.define(nginx);
return [];
}
function readOnlyExtensions(readOnly: boolean) {
return [EditorState.readOnly.of(readOnly), EditorView.editable.of(!readOnly)];
}
onMounted(() => {
if (!host.value) return;
view = new EditorView({
parent: host.value,
state: EditorState.create({
doc: props.modelValue,
extensions: [
basicSetup,
languageCompartment.of(languageExtension(props.format)),
readOnlyCompartment.of(readOnlyExtensions(props.readOnly)),
EditorView.lineWrapping,
EditorView.theme({
"&": { height: "100%", fontSize: "13px", backgroundColor: "transparent" },
".cm-scroller": { fontFamily: "var(--dbx-editor-font-family, ui-monospace)", overflow: "auto" },
".cm-content": { minHeight: "13rem" },
"&.cm-focused": { outline: "none" },
}),
EditorView.updateListener.of((update) => {
if (update.docChanged) emit("update:modelValue", update.state.doc.toString());
}),
],
}),
});
});
watch(
() => props.modelValue,
(value) => {
if (!view || view.state.doc.toString() === value) return;
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: value } });
},
);
watch(
() => props.format,
(format) => view?.dispatch({ effects: languageCompartment.reconfigure(languageExtension(format)) }),
);
watch(
() => props.readOnly,
(readOnly) => view?.dispatch({ effects: readOnlyCompartment.reconfigure(readOnlyExtensions(readOnly)) }),
);
onBeforeUnmount(() => {
view?.destroy();
view = null;
});
</script>
<template>
<div ref="host" class="min-h-52 overflow-hidden rounded-md border border-input bg-background" />
</template>
@@ -0,0 +1,42 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
const browserSource = readFileSync(new URL("../KvKeyBrowser.vue", import.meta.url), "utf8");
const etcdBrowserSource = readFileSync(new URL("../../etcd/EtcdKeyBrowser.vue", import.meta.url), "utf8");
describe("KvKeyBrowser node export", () => {
it("shows export for both value keys and virtual directories", () => {
expect(browserSource).toContain("if (props.api.exportScope || nodeHasValue(node))");
expect(browserSource).toContain('kind: nodeIsExpandable(node) ? "prefix" : "key"');
expect(browserSource).toContain("await props.api.exportScope(props.connectionId, request)");
});
it("delegates etcd directory export to a fixed-revision recursive scan", () => {
expect(etcdBrowserSource).toContain("exportScope: exportEtcdNodeScope");
expect(etcdBrowserSource).toContain("const scan = await scanConnection(connectionId, request.path)");
expect(etcdBrowserSource).toContain("isKeyInKvExportScope(displayKey(keyValue(entry), entry.key), request)");
expect(etcdBrowserSource).toContain("const missingValue = entries.find((entry) => !entry.value)");
});
it("keeps mirror deletes inside the exported directory and compares canonical Key bytes", () => {
expect(etcdBrowserSource).toContain("if (!isKeyInKvExportScope(shown, mirrorScope)");
expect(etcdBrowserSource).toContain("sourceKeys.has(kvValueByteIdentity(bytes))");
expect(etcdBrowserSource).toContain("id: `source:${kvValueByteIdentity(source.key)}`");
expect(etcdBrowserSource).not.toContain("id: source.key.data");
expect(etcdBrowserSource).toContain('const mirrorDeleteAvailable = computed(() => transferBundle.value?.scopeKind === "prefix")');
expect(etcdBrowserSource).toContain(':disabled="!mirrorDeleteAvailable || transferLoading || transferApplying"');
});
it("snapshots the target and invalidates stale transfer previews", () => {
expect(etcdBrowserSource).toContain("const targetId = targetConnectionId.value");
expect(etcdBrowserSource).toContain("const generation = ++transferPreviewGeneration");
expect(etcdBrowserSource).toContain("if (generation !== transferPreviewGeneration) return");
expect(etcdBrowserSource).toContain(':disabled="transferLoading || transferApplying"');
});
it("refreshes a partial batch before allowing the remaining operations to retry", () => {
expect(etcdBrowserSource).toContain('row.operation = "applied"');
expect(etcdBrowserSource).toContain("await previewTransfer()");
expect(etcdBrowserSource).toContain('t("etcd.transferPartiallyApplied"');
});
});
@@ -0,0 +1,23 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
const browserSource = readFileSync(new URL("../KvKeyBrowser.vue", import.meta.url), "utf8");
describe("KvKeyBrowser edit dialog layout", () => {
it("keeps the value format label and selector together in the editor toolbar", () => {
expect(browserSource).toContain('class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between"');
expect(browserSource).toContain('class="flex shrink-0 items-center gap-3"');
expect(browserSource).toContain('<SelectTrigger class="h-9 w-44">');
});
it("cancels the shared footer negative margin and preserves bottom safe spacing", () => {
expect(browserSource).toContain('<DialogFooter class="mx-0 mb-0 shrink-0 gap-3 border-t bg-muted/10 px-6 py-5">');
expect(browserSource).toContain('variant="outline" class="h-10 min-w-20"');
expect(browserSource).toContain('<Button class="h-10 min-w-20" :disabled="saving || readOnly"');
});
it("applies the same safe spacing to the history dialog footer", () => {
expect(browserSource).toContain('<DialogFooter class="mx-0 mb-0 shrink-0 border-t px-6 py-5">');
expect(browserSource).toContain('class="h-10 min-w-20" @click="showHistoryDialog = false"');
});
});
@@ -0,0 +1,17 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
const browserSource = readFileSync(new URL("../KvKeyBrowser.vue", import.meta.url), "utf8");
describe("KvKeyBrowser mutation errors", () => {
it("shows duplicate-key conflicts next to the Key field", () => {
expect(browserSource).toContain("classifyKvMutationError(error, isCreating.value");
expect(browserSource).toContain(`:aria-invalid="editErrorKind === 'keyAlreadyExists'"`);
expect(browserSource).toContain(`v-if="editError && editErrorKind === 'keyAlreadyExists'"`);
});
it("clears the duplicate warning after the Key name changes", () => {
expect(browserSource).toContain("watch(editKey, () =>");
expect(browserSource).toContain(`if (editErrorKind.value !== "keyAlreadyExists") return`);
});
});
@@ -0,0 +1,20 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
const browserSource = readFileSync(new URL("../KvKeyBrowser.vue", import.meta.url), "utf8");
describe("KvKeyBrowser search and split layout", () => {
it("uses the requested prefix for a server-side, paginated search", () => {
expect(browserSource).toContain("props.api.listPrefix(connectionId, searchQuery, pageSize, reset ? null : continuation.value");
expect(browserSource).not.toContain("async function searchAllKeys");
expect(browserSource).not.toContain("filterKvKeysBySearch(result.keys, query)");
});
it("uses a persisted draggable split between the Key tree and details", () => {
expect(browserSource).toContain('<Splitpanes class="kv-browser-splitpanes min-h-0 flex-1" @resized="handleKvBrowserSplitResized">');
expect(browserSource).toContain('<Pane :size="kvBrowserSplitSize" min-size="20" max-size="70">');
expect(browserSource).toContain('<Pane :size="100 - kvBrowserSplitSize" min-size="30">');
expect(browserSource).toContain("safeLocalStorageSet(kvBrowserSplitSizeStorageKey, String(size))");
expect(browserSource).toContain("cursor: col-resize");
});
});
@@ -0,0 +1,19 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
const browserSource = readFileSync(new URL("../KvKeyBrowser.vue", import.meta.url), "utf8");
describe("KvKeyBrowser TTL details", () => {
it("renders the selected key TTL and refreshes it every second", () => {
expect(browserSource).toContain("const selectedTtlLabel = computed");
expect(browserSource).toContain('<Badge v-if="selectedTtlLabel" variant="outline">TTL {{ selectedTtlLabel }}</Badge>');
expect(browserSource).toContain("metadataRefreshTimer = setInterval");
expect(browserSource).toContain("void refreshSelectedMetadata(key, generation)");
});
it("stops TTL polling when the browser unmounts", () => {
expect(browserSource).toContain("onBeforeUnmount(() =>");
expect(browserSource).toContain("stopKeyListRefresh()");
expect(browserSource).toContain("stopMetadataRefresh()");
});
});