Skip to content

Commit 94a5d9b

Browse files
committed
test(usage-bar): add regression tests for prototype key handling
Add focused tests proving Object.hasOwn correctly distinguishes own properties from inherited Object.prototype keys (toString, constructor) in both alias vocabulary lookup and segment case table lookup.
1 parent 5430d56 commit 94a5d9b

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/auto-reply/usage-bar/translator.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ describe("usage-bar verbs", () => {
6565
expect(render([{ text: "{m|alias:models}" }], { m: "some-new-model" })).toBe("some-new-model");
6666
});
6767

68+
it("alias — prototype keys do not match inherited properties", () => {
69+
const t = tpl([{ text: "{m|alias:models}" }]);
70+
// "toString" with explicit alias entry → returns the aliased value
71+
t.aliases.models["toString"] = "aliased";
72+
expect(renderUsageBar(t, { surface: "discord", m: "toString" })).toBe("aliased");
73+
// "constructor" without alias entry → passes through, does not hit Object.prototype
74+
expect(renderUsageBar(t, { surface: "discord", m: "constructor" })).toBe("constructor");
75+
});
76+
6877
it("fallback when path is missing/empty", () => {
6978
expect(render([{ text: "{identity.emoji|🤖} hi" }], {})).toBe("🤖 hi");
7079
expect(render([{ text: "{identity.emoji|🤖} hi" }], { identity: { emoji: "🩺" } })).toBe(
@@ -80,6 +89,22 @@ describe("usage-bar segment forms", () => {
8089
expect(render(seg, { u: { cache_hit_pct: 0 } })).toBe("🗄 0%");
8190
});
8291

92+
it("map — prototype keys do not match inherited properties, fallback to _default", () => {
93+
const seg = [
94+
{
95+
map: "key",
96+
cases: { true: "yes", false: "no", _default: "fallback" },
97+
},
98+
];
99+
// "toString" is an Object.prototype property — should NOT match, fallback to _default
100+
expect(render(seg, { key: "toString" })).toBe("fallback");
101+
// "constructor" is also inherited — should NOT match
102+
expect(render(seg, { key: "constructor" })).toBe("fallback");
103+
// Normal values still match correctly
104+
expect(render(seg, { key: "true" })).toBe("yes");
105+
expect(render(seg, { key: "other" })).toBe("fallback");
106+
});
107+
83108
it("map resolves enum/bool, drops on no match", () => {
84109
const seg = [{ map: "state.fast_mode", cases: { true: "⚡", false: "🐌" } }];
85110
expect(render(seg, { state: { fast_mode: true } })).toBe("⚡");

0 commit comments

Comments
 (0)