Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
140 lines
4.6 KiB
TypeScript
140 lines
4.6 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import React from "react";
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
|
|
import { I18nProvider, useI18n } from "../application/i18n/I18nProvider.tsx";
|
|
import type { Snippet } from "../types.ts";
|
|
import { Dialog } from "./ui/dialog.tsx";
|
|
import SnippetsManager, {
|
|
SNIPPET_IMPORT_SAMPLE_FILES,
|
|
SnippetImportDialogContent,
|
|
buildSnippetImportSamplesZip,
|
|
} from "./SnippetsManager.tsx";
|
|
|
|
const installStorageStub = () => {
|
|
const values = new Map<string, string>();
|
|
Object.defineProperty(globalThis, "localStorage", {
|
|
configurable: true,
|
|
value: {
|
|
getItem: (key: string) => values.get(key) ?? null,
|
|
setItem: (key: string, value: string) => {
|
|
values.set(key, value);
|
|
},
|
|
removeItem: (key: string) => {
|
|
values.delete(key);
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
const snippet: Snippet = {
|
|
id: "snippet-1",
|
|
label: "Restart nginx",
|
|
command: "sudo systemctl restart nginx",
|
|
tags: [],
|
|
package: "ops",
|
|
targets: ["host-1"],
|
|
};
|
|
|
|
test("SnippetsManager renders import and multi-select controls", () => {
|
|
installStorageStub();
|
|
const noop = () => {};
|
|
|
|
const markup = renderToStaticMarkup(
|
|
<I18nProvider locale="en">
|
|
<SnippetsManager
|
|
snippets={[snippet]}
|
|
packages={["ops"]}
|
|
hosts={[]}
|
|
hotkeyScheme="mac"
|
|
keyBindings={[]}
|
|
onSave={noop}
|
|
onBulkSave={noop}
|
|
onDelete={noop}
|
|
onPackagesChange={noop}
|
|
/>
|
|
</I18nProvider>,
|
|
);
|
|
|
|
assert.match(markup, /Import/);
|
|
assert.match(markup, /Select snippets/);
|
|
assert.doesNotMatch(markup, /Snippet import format/);
|
|
});
|
|
|
|
test("SnippetImportDialog shows example JSON before import confirmation", () => {
|
|
const noop = () => {};
|
|
|
|
const TestDialog = () => {
|
|
const { t } = useI18n();
|
|
const fileInputRef = React.createRef<HTMLInputElement>();
|
|
return (
|
|
<Dialog open={true}>
|
|
<SnippetImportDialogContent
|
|
pendingImport={null}
|
|
t={t}
|
|
fileInputRef={fileInputRef}
|
|
onFileSelected={noop}
|
|
onChooseFile={noop}
|
|
onConfirmSkip={noop}
|
|
onConfirmOverwrite={noop}
|
|
onCancel={noop}
|
|
onDownloadExamples={noop}
|
|
/>
|
|
</Dialog>
|
|
);
|
|
};
|
|
|
|
const markup = renderToStaticMarkup(
|
|
<I18nProvider locale="en">
|
|
<TestDialog />
|
|
</I18nProvider>,
|
|
);
|
|
|
|
assert.match(markup, /Example JSON/);
|
|
assert.match(markup, /netcatty\.snippets/);
|
|
assert.match(markup, /JSON array/);
|
|
assert.match(markup, /Choose file/);
|
|
assert.match(markup, /Download samples/);
|
|
assert.match(markup, /Confirm import/);
|
|
assert.match(markup, /multiple=""/);
|
|
});
|
|
|
|
test("snippet import sample files include standard and array formats", () => {
|
|
assert.equal(SNIPPET_IMPORT_SAMPLE_FILES.length, 5);
|
|
assert.ok(SNIPPET_IMPORT_SAMPLE_FILES.some((file) => file.name === "01-standard-netcatty-object.json"));
|
|
assert.ok(SNIPPET_IMPORT_SAMPLE_FILES.some((file) => file.name === "02-plain-snippet-array.json"));
|
|
assert.deepEqual(JSON.parse(SNIPPET_IMPORT_SAMPLE_FILES[1].content).map((item: { command: string }) => item.command), [
|
|
"ss -lntp",
|
|
"pwd",
|
|
]);
|
|
});
|
|
|
|
test("buildSnippetImportSamplesZip returns a zip archive", async () => {
|
|
const blob = buildSnippetImportSamplesZip();
|
|
const bytes = new Uint8Array(await blob.arrayBuffer());
|
|
|
|
assert.equal(blob.type, "application/zip");
|
|
assert.deepEqual(Array.from(bytes.slice(0, 4)), [0x50, 0x4b, 0x03, 0x04]);
|
|
});
|
|
|
|
const snippetsManagerSource = readFileSync(new URL("./SnippetsManager.tsx", import.meta.url), "utf8");
|
|
|
|
test("vault snippet package and snippet cards use asChild context-menu triggers", () => {
|
|
assert.equal(snippetsManagerSource.match(/<ContextMenuTrigger asChild>/g)?.length, 2);
|
|
assert.doesNotMatch(snippetsManagerSource, /<ContextMenuTrigger>(?! asChild)/);
|
|
assert.match(snippetsManagerSource, /from '\.\/ui\/primaryOnlyDrag'/);
|
|
assert.match(snippetsManagerSource, /primaryOnlyDragHandlers/);
|
|
assert.match(snippetsManagerSource, /deleteSnippetPackage/);
|
|
assert.match(snippetsManagerSource, /renameSnippetPackage/);
|
|
assert.match(snippetsManagerSource, /SNIPPET_PACKAGE_PATH_CHANGE_EVENT/);
|
|
assert.match(snippetsManagerSource, /applySnippetPackagePathChange/);
|
|
assert.match(snippetsManagerSource, /setSelectedPackage/);
|
|
assert.match(snippetsManagerSource, /setEditingSnippet/);
|
|
assert.match(snippetsManagerSource, /setIsRenameDialogOpen\(false\)/);
|
|
assert.match(snippetsManagerSource, /setRenamingPackagePath\(null\)/);
|
|
assert.match(snippetsManagerSource, /deleteTarget\?\.type === 'package'/);
|
|
assert.match(snippetsManagerSource, /setDeleteTarget\(null\)/);
|
|
});
|