Skip to content

Commit a1c0e0e

Browse files
author
Nic Bradley
committed
libSmartAttributes | fixing tests
1 parent 5ef13d5 commit a1c0e0e

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

libSmartAttributes/tests/index.test.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe("SmartAttributes", () => {
6060
const result = await SmartAttributes.getAttribute(characterId, attributeName);
6161

6262
expect(mockFindObjs).toHaveBeenCalled();
63-
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, attributeName);
63+
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, attributeName, "current");
6464
expect(result).toBe("beacon-value");
6565
});
6666

@@ -70,8 +70,8 @@ describe("SmartAttributes", () => {
7070

7171
const result = await SmartAttributes.getAttribute(characterId, attributeName);
7272

73-
expect(mockGetSheetItem).toHaveBeenNthCalledWith(1, characterId, attributeName);
74-
expect(mockGetSheetItem).toHaveBeenNthCalledWith(2, characterId, `user.${attributeName}`);
73+
expect(mockGetSheetItem).toHaveBeenNthCalledWith(1, characterId, attributeName, "current");
74+
expect(mockGetSheetItem).toHaveBeenNthCalledWith(2, characterId, `user.${attributeName}`, "current");
7575
expect(result).toBe("user-value");
7676
});
7777

@@ -87,24 +87,24 @@ describe("SmartAttributes", () => {
8787

8888
it("should handle falsy beacon values correctly", async () => {
8989
mockFindObjs.mockReturnValue([]);
90-
mockGetSheetItem.mockResolvedValueOnce(0).mockResolvedValueOnce(null); // 0 is falsy, so code continues to user attr
90+
mockGetSheetItem.mockResolvedValueOnce(0); // 0 is now treated as valid
9191

9292
const result = await SmartAttributes.getAttribute(characterId, attributeName);
9393

94-
expect(result).toBeUndefined(); // Since user attr also returns null
95-
expect(mockGetSheetItem).toHaveBeenCalledTimes(2);
96-
expect(mockLog).toHaveBeenCalledWith(`Attribute ${attributeName} not found on character ${characterId}`);
94+
expect(result).toBe(0); // 0 is returned as valid beacon value
95+
expect(mockGetSheetItem).toHaveBeenCalledTimes(1);
96+
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, attributeName, "current");
9797
});
9898

9999
it("should handle empty string beacon values correctly", async () => {
100100
mockFindObjs.mockReturnValue([]);
101-
mockGetSheetItem.mockResolvedValueOnce("").mockResolvedValueOnce(null); // '' is falsy, so code continues to user attr
101+
mockGetSheetItem.mockResolvedValueOnce(""); // '' is now treated as valid
102102

103103
const result = await SmartAttributes.getAttribute(characterId, attributeName);
104104

105-
expect(result).toBeUndefined(); // Since user attr also returns null
106-
expect(mockGetSheetItem).toHaveBeenCalledTimes(2);
107-
expect(mockLog).toHaveBeenCalledWith(`Attribute ${attributeName} not found on character ${characterId}`);
105+
expect(result).toBe(""); // Empty string is returned as valid beacon value
106+
expect(mockGetSheetItem).toHaveBeenCalledTimes(1);
107+
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, attributeName, "current");
108108
});
109109
});
110110

@@ -126,7 +126,7 @@ describe("SmartAttributes", () => {
126126
name: attributeName
127127
});
128128
expect(mockAttr.set).toHaveBeenCalledWith({ current: value });
129-
expect(result).toBe(value);
129+
expect(result).toBeUndefined();
130130
});
131131

132132
it("should set legacy attribute max value when type is specified", async () => {
@@ -137,7 +137,7 @@ describe("SmartAttributes", () => {
137137
const result = await SmartAttributes.setAttribute(characterId, attributeName, value, "max");
138138

139139
expect(mockAttr.set).toHaveBeenCalledWith({ max: value });
140-
expect(result).toBe(value);
140+
expect(result).toBeUndefined();
141141
});
142142

143143
it("should set beacon computed attribute when no legacy attribute but beacon exists", async () => {
@@ -147,9 +147,9 @@ describe("SmartAttributes", () => {
147147

148148
const result = await SmartAttributes.setAttribute(characterId, attributeName, value);
149149

150-
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, attributeName);
150+
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, attributeName, "current");
151151
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, attributeName, value);
152-
expect(result).toBe("updated-value");
152+
expect(result).toBeUndefined();
153153
});
154154

155155
it("should default to user attribute when no legacy or beacon attribute exists", async () => {
@@ -159,8 +159,8 @@ describe("SmartAttributes", () => {
159159

160160
const result = await SmartAttributes.setAttribute(characterId, attributeName, value);
161161

162-
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, value);
163-
expect(result).toBe("user-value");
162+
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, value, "current");
163+
expect(result).toBeUndefined();
164164
});
165165

166166
it("should handle complex values correctly", async () => {
@@ -171,8 +171,8 @@ describe("SmartAttributes", () => {
171171

172172
const result = await SmartAttributes.setAttribute(characterId, attributeName, complexValue);
173173

174-
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, complexValue);
175-
expect(result).toBe(complexValue);
174+
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, complexValue, "current");
175+
expect(result).toBeUndefined();
176176
});
177177

178178
it("should handle null and undefined values", async () => {
@@ -182,35 +182,35 @@ describe("SmartAttributes", () => {
182182

183183
const result = await SmartAttributes.setAttribute(characterId, attributeName, null);
184184

185-
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, null);
186-
expect(result).toBe(null);
185+
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, null, "current");
186+
expect(result).toBeUndefined();
187187
});
188188

189189
it("should handle falsy beacon values correctly for setting", async () => {
190190
mockFindObjs.mockReturnValue([]);
191-
mockGetSheetItem.mockResolvedValue(0); // falsy but valid existing value - code treats as falsy so goes to user path
191+
mockGetSheetItem.mockResolvedValue(0); // 0 is now treated as valid existing beacon value
192192
mockSetSheetItem.mockResolvedValue("updated");
193193

194194
const result = await SmartAttributes.setAttribute(characterId, attributeName, value);
195195

196-
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, value);
197-
expect(result).toBe("updated");
196+
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, attributeName, value);
197+
expect(result).toBeUndefined();
198198
});
199199
});
200200

201201
describe("edge cases", () => {
202202
const characterId = "char123";
203203
const attributeName = "test-attr";
204204

205-
it("should handle user attribute with truthy value after falsy beacon", async () => {
205+
it("should handle user attribute when beacon returns null", async () => {
206206
mockFindObjs.mockReturnValue([]);
207-
mockGetSheetItem.mockResolvedValueOnce(0).mockResolvedValueOnce("user-value");
207+
mockGetSheetItem.mockResolvedValueOnce(null).mockResolvedValueOnce("user-value");
208208

209209
const result = await SmartAttributes.getAttribute(characterId, attributeName);
210210

211211
expect(result).toBe("user-value");
212-
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, attributeName);
213-
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`);
212+
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, attributeName, "current");
213+
expect(mockGetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, "current");
214214
});
215215

216216
it("should handle numeric values in attributes", async () => {
@@ -224,11 +224,11 @@ describe("SmartAttributes", () => {
224224

225225
it("should handle boolean values in attributes", async () => {
226226
mockFindObjs.mockReturnValue([]);
227-
mockGetSheetItem.mockResolvedValueOnce(true);
227+
mockGetSheetItem.mockResolvedValueOnce(false); // Test with false to show falsy values are valid
228228

229229
const result = await SmartAttributes.getAttribute(characterId, attributeName);
230230

231-
expect(result).toBe(true);
231+
expect(result).toBe(false);
232232
});
233233
});
234234

@@ -247,7 +247,7 @@ describe("SmartAttributes", () => {
247247
// Set new value
248248
mockAttr.set.mockReturnValue("15");
249249
const result = await SmartAttributes.setAttribute(characterId, attributeName, "15");
250-
expect(result).toBe("15");
250+
expect(result).toBeUndefined();
251251
});
252252

253253
it("should handle complete workflow from get to set with beacon attributes", async () => {
@@ -261,7 +261,7 @@ describe("SmartAttributes", () => {
261261

262262
// Set new value
263263
const result = await SmartAttributes.setAttribute(characterId, attributeName, "beacon-15");
264-
expect(result).toBe("beacon-15");
264+
expect(result).toBeUndefined();
265265
});
266266

267267
it("should handle get returning undefined but set still working", async () => {
@@ -275,8 +275,8 @@ describe("SmartAttributes", () => {
275275

276276
// But set still works by creating user attribute
277277
const result = await SmartAttributes.setAttribute(characterId, attributeName, "new-value");
278-
expect(result).toBe("new-value");
279-
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, "new-value");
278+
expect(result).toBeUndefined();
279+
expect(mockSetSheetItem).toHaveBeenCalledWith(characterId, `user.${attributeName}`, "new-value", "current");
280280
});
281281
});
282282
});

0 commit comments

Comments
 (0)