diff --git a/src/components/chests/chests.spec.ts b/src/components/chests/chests.spec.ts index 3582724..b6c644c 100644 --- a/src/components/chests/chests.spec.ts +++ b/src/components/chests/chests.spec.ts @@ -60,6 +60,20 @@ describe('Chests', () => { expect(errorTrackerMock.Object.track).not.toHaveBeenCalled(); }); + it('patches doItOrigin without replacing an existing toolkit wrapper', () => { + setup('function toolkitWrapper() {}'); + const wrapper = itemUse.doIt; + itemUse.doItOrigin = doItWithSource( + "function (itemId, itemCount) { EventHandler.signal('item_used', [itemId]); }", + ); + + chests.init(); + + expect(itemUse.doIt).toBe(wrapper); + expect(itemUse.doItOrigin.toString()).toContain('TW_Calc.trackChest(itemId,res);'); + expect(errorTrackerMock.Object.track).not.toHaveBeenCalled(); + }); + it('tracks an error and leaves the handler untouched when the anchor is missing', () => { setup("function (itemId, itemCount) { EventHandler.signal('something_else', [itemId]); }"); const original = itemUse.doIt; diff --git a/src/components/chests/chests.ts b/src/components/chests/chests.ts index 366561e..7f2ea2e 100644 --- a/src/components/chests/chests.ts +++ b/src/components/chests/chests.ts @@ -35,7 +35,7 @@ export class Chests implements Component { const pos = match.index; const body = str.substr(0, pos) + 'TW_Calc.trackChest(itemId,res);' + str.substr(pos); this.logger.log('patching the chest handler...', body); - newStr = 'ItemUse.doIt = ' + body; + newStr = `ItemUse.${toolkit} = ${body}`; eval(newStr); } catch (e: unknown) { const error = e as Error;