Repository navigation
feat(dashboard): inline show/hide toggle for API keys on API Manager page #4505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { describe, it } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { | ||
| maskKey, | ||
| toggleKeyVisibility, | ||
| } from "../../src/app/(dashboard)/dashboard/api-manager/apiManagerPageUtils.js"; | ||
|
|
||
| describe("maskKey", () => { | ||
| it("returns empty string when key is missing or empty", () => { | ||
| assert.equal(maskKey(""), ""); | ||
| assert.equal(maskKey(null), ""); | ||
| assert.equal(maskKey(undefined), ""); | ||
| }); | ||
|
|
||
| it("returns the key untouched when it fits the visible budget (<=8 chars)", () => { | ||
| assert.equal(maskKey("sk"), "sk"); | ||
| assert.equal(maskKey("sk-12345"), "sk-12345"); | ||
| }); | ||
|
|
||
| it("keeps the first 8 chars and appends an ellipsis when the key is longer", () => { | ||
| const full = "sk-or-1234567890abcdef"; | ||
| const masked = maskKey(full); | ||
| assert.equal(masked.startsWith("sk-or-12"), true); | ||
| assert.equal(masked.endsWith("..."), true); | ||
| // Must not leak the tail | ||
| assert.equal(masked.includes("90abcdef"), false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("toggleKeyVisibility", () => { | ||
| it("adds an id when it is not present", () => { | ||
| const next = toggleKeyVisibility(new Set<string>(), "k1"); | ||
| assert.equal(next.has("k1"), true); | ||
| assert.equal(next.size, 1); | ||
| }); | ||
|
|
||
| it("removes an id when it is already present", () => { | ||
| const next = toggleKeyVisibility(new Set<string>(["k1", "k2"]), "k1"); | ||
| assert.equal(next.has("k1"), false); | ||
| assert.equal(next.has("k2"), true); | ||
| assert.equal(next.size, 1); | ||
| }); | ||
|
|
||
| it("returns a NEW Set (does not mutate the input — React state safety)", () => { | ||
| const input = new Set<string>(["k1"]); | ||
| const output = toggleKeyVisibility(input, "k2"); | ||
| assert.notEqual(output, input); | ||
| assert.equal(input.has("k2"), false); | ||
| assert.equal(output.has("k2"), true); | ||
| }); | ||
| }); | ||
|
Comment on lines
+1
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the project uses Vitest (as indicated by Here is how the test file can be refactored to use Vitest: import { describe, it, expect } from "vitest";
import {
maskKey,
toggleKeyVisibility,
} from "../../src/app/(dashboard)/dashboard/api-manager/apiManagerPageUtils.js";
describe("maskKey", () => {
it("returns empty string when key is missing or empty", () => {
expect(maskKey("")).toBe("");
expect(maskKey(null)).toBe("");
expect(maskKey(undefined)).toBe("");
});
it("returns the key untouched when it fits the visible budget (<=8 chars)", () => {
expect(maskKey("sk")).toBe("sk");
expect(maskKey("sk-12345")).toBe("sk-12345");
});
it("keeps the first 8 chars and appends an ellipsis when the key is longer", () => {
const full = "sk-or-1234567890abcdef";
const masked = maskKey(full);
expect(masked.startsWith("sk-or-12")).toBe(true);
expect(masked.endsWith("...")).toBe(true);
expect(masked.includes("90abcdef")).toBe(false);
});
});
describe("toggleKeyVisibility", () => {
it("adds an id when it is not present", () => {
const next = toggleKeyVisibility(new Set<string>(), "k1");
expect(next.has("k1")).toBe(true);
expect(next.size).toBe(1);
});
it("removes an id when it is already present", () => {
const next = toggleKeyVisibility(new Set<string>(["k1", "k2"]), "k1");
expect(next.has("k1")).toBe(false);
expect(next.has("k2")).toBe(true);
expect(next.size).toBe(1);
});
it("returns a NEW Set (does not mutate the input — React state safety)", () => {
const input = new Set<string>(["k1"]);
const output = toggleKeyVisibility(input, "k2");
expect(output).not.toBe(input);
expect(input.has("k2")).toBe(false);
expect(output.has("k2")).toBe(true);
});
}); |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the API key is already revealed and cached in
revealedKeys, we can copy it directly from the client-side cache instead of making an unnecessary network request to/api/keys/{id}/reveal.