Files
wehub-resource-sync adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:17:32 +08:00

84 lines
2.6 KiB
TypeScript

import { describe, test, expect } from "vitest";
import { encrypt, decrypt } from "./crypto";
describe("encrypt", () => {
test("returns a string in ivString:cipherString format", () => {
const result = encrypt("hello", "my-secret");
const parts = result.split(":");
expect(parts.length).toBe(2);
expect(parts[0].length).toBeGreaterThan(0);
expect(parts[1].length).toBeGreaterThan(0);
});
test("output differs from the original input", () => {
const input = "hello world";
expect(encrypt(input, "secret")).not.toBe(input);
});
test("two encryptions of the same data produce different ciphertext (random IV)", () => {
const data = "same input data";
const key = "same-key";
expect(encrypt(data, key)).not.toBe(encrypt(data, key));
});
test("different keys produce different ciphertext for the same data", () => {
const data = "same data";
expect(encrypt(data, "key-A")).not.toBe(encrypt(data, "key-B"));
});
});
describe("decrypt", () => {
test("round-trips a plain string value", () => {
const original = "hello world";
const key = "my-secret-key";
expect(decrypt(encrypt(original, key), key)).toBe(original);
});
test("round-trips a JSON-stringified object", () => {
const obj = { user: "alice", count: 42 };
const key = "secret";
const roundtripped = JSON.parse(
decrypt(encrypt(JSON.stringify(obj), key), key)
);
expect(roundtripped).toEqual(obj);
});
test("round-trips a JSON-stringified array", () => {
const arr = [1, "two", true, null];
const key = "secret";
const roundtripped = JSON.parse(
decrypt(encrypt(JSON.stringify(arr), key), key)
);
expect(roundtripped).toEqual(arr);
});
test("round-trips special characters and unicode", () => {
const unicode = "héllo wörld 🎉 日本語";
const key = "secret";
expect(decrypt(encrypt(unicode, key), key)).toBe(unicode);
});
test("round-trips a large payload without truncation", () => {
const large = JSON.stringify({ data: "x".repeat(10_000) });
const key = "key";
expect(decrypt(encrypt(large, key), key)).toBe(large);
});
test("wrong key does not produce the original plaintext", () => {
const original = "sensitive data";
const encrypted = encrypt(original, "correct-key");
const result = decrypt(encrypted, "wrong-key");
expect(result).not.toBe(original);
});
test("completely malformed input (no IV separator) does not return the input as-is", () => {
// CryptoJS may throw or return "" — either is acceptable; it must not echo the input
try {
const result = decrypt("not-valid-encrypted-data", "key");
expect(result).not.toBe("not-valid-encrypted-data");
} catch {
// Throwing is also acceptable
}
});
});