From f8ca4148f0c3b942a8ffa43466201c44a3b1926d Mon Sep 17 00:00:00 2001 From: xenticore Date: Fri, 28 Mar 2025 23:40:47 -0400 Subject: [PATCH] feat: schema now retrieves slot, australium status, killstreak status --- __tests__/schema.test.ts | 103 ++++++++++++++++++++++++++++++++--- src/content/schemaService.ts | 54 ++++++++++++++++-- 2 files changed, 145 insertions(+), 12 deletions(-) diff --git a/__tests__/schema.test.ts b/__tests__/schema.test.ts index 2f34c1d..34fb440 100644 --- a/__tests__/schema.test.ts +++ b/__tests__/schema.test.ts @@ -1,16 +1,57 @@ -import { describe, expect, test } from "bun:test"; -import { ItemSchema, getItemIndexByName, getTradableStatusByDefindex, getTradableStatusByName} from '../src/content/schemaService' +import { describe, expect, test, mock } from "bun:test"; +import { ItemSchema, ItemSlot, getItemIndexByName, getTradableStatusByDefindex, getTradableStatusByName, prepareSchema } from '../src/content/schemaService' + +// Mock the storage and log functions +mock.module('../src/content/storage', () => ({ + setStorageValue: mock(async (key: string, value: any) => {}), + getStorageValue: mock(async (key: string, defaultValue: any) => defaultValue) +})); + +mock.module('../src/content/utils/log', () => ({ + logDebug: mock(() => {}), + log: mock(() => {}), + logError: mock(() => {}) +})); const mockSchema: ItemSchema = { - '21': { name: 'Flame Thrower', tradable: false }, - '208': { name: 'Flame Thrower', tradable: true }, - '5021': { name: 'Mann Co. Supply Crate Key', tradable: true }, - '15141': { name: 'Flame Thrower', tradable: true }, - '69420': { name: 'Non-Tradable Item', tradable: false } + '21': { + name: 'Flame Thrower', + slot: ItemSlot.Primary, + tradable: false, + hasAustraliumVariant: false, + canKillstreakify: true + }, + '208': { + name: 'Flame Thrower', + slot: ItemSlot.Primary, + tradable: true, + hasAustraliumVariant: true, + canKillstreakify: true + }, + '5021': { + name: 'Mann Co. Supply Crate Key', + slot: ItemSlot.Tool, + tradable: true, + hasAustraliumVariant: false, + canKillstreakify: false + }, + '15141': { + name: 'Flame Thrower', + slot: ItemSlot.Primary, + tradable: true, + hasAustraliumVariant: false, + canKillstreakify: true + }, + '69420': { + name: 'Non-Tradable Item', + slot: ItemSlot.Misc, + tradable: false, + hasAustraliumVariant: false, + canKillstreakify: false + } } describe('Schema Service', () => { - test('getItemIndexByName returns correct defindex', () => { expect(getItemIndexByName(mockSchema, 'Flame Thrower')).toBe(208) expect(getItemIndexByName(mockSchema, 'Mann Co. Supply Crate Key')).toBe(5021) @@ -30,4 +71,50 @@ describe('Schema Service', () => { expect(getTradableStatusByName(mockSchema, 'Non-Tradable Item')).toBe(false) expect(getTradableStatusByName(mockSchema, 'Non-Existent Item')).toBe(true) }) + + test('prepareSchema fetches and processes schema correctly', async () => { + // Mock GM_fetch response + const mockResponse = { + ok: true, + json: async () => [ + { + defindex: 1, + item_name: 'Test Item', + item_slot: 'misc', + attributes: [ + { "name": "cannot trade", "class": "cannot_trade", "value": 1 } + ], + capabilities: {} + }, + { + defindex: 208, + item_name: 'Flame Thrower', + item_slot: 'primary', + capabilities: { can_killstreakify: true } + } + ] + }; + + // Mock GM_fetch + globalThis.GM_fetch = mock(async () => mockResponse); + + const result = await prepareSchema(); + + expect(result).toEqual({ + '1': { + name: 'Test Item', + slot: ItemSlot.Misc, + tradable: false, + canKillstreakify: false, + hasAustraliumVariant: false + }, + '208': { + name: 'Flame Thrower', + slot: ItemSlot.Primary, + tradable: true, + canKillstreakify: true, + hasAustraliumVariant: true + } + }); + }); }) \ No newline at end of file diff --git a/src/content/schemaService.ts b/src/content/schemaService.ts index 1c6e847..e86a2f0 100644 --- a/src/content/schemaService.ts +++ b/src/content/schemaService.ts @@ -4,8 +4,13 @@ import './config' declare function GM_fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise import './GM_fetch' import { storage_version, storage_schema, storage_lastUpdateTime } from './config' +import Australiums from '../resources/australiums.json' const semver = require('semver') +export function checkAustraliumVariant(defindex: number): boolean { + return Object.prototype.hasOwnProperty.call(Australiums, defindex.toString()); +} + export declare const __VERSION__: string; function isDateAfterOneDay(date1: Date, date2: Date): boolean { @@ -14,7 +19,28 @@ function isDateAfterOneDay(date1: Date, date2: Date): boolean { return diffDays > 1; } -export class ItemSchema { [key: string]: {name: string, tradable: Boolean}; } +export enum ItemSlot { + Primary = "primary", + Secondary = "secondary", + Melee = "melee", + PDA = "pda", + PDA2 = "pda2", + Building = "building", + Misc = "misc", + Special = "special", + Taunt = "taunt", + Tool = "tool", +} + +export class ItemSchema { + [key: string]: { + name: string, + slot: ItemSlot, + tradable: Boolean, + hasAustraliumVariant: Boolean, + canKillstreakify: Boolean + }; +} export function getItemIndexByName(schema: ItemSchema, name: string, excludeStock: Boolean = true, excludeDecorated: Boolean = true) { for (const [defindex, value] of Object.entries(schema)) { @@ -63,6 +89,7 @@ export async function prepareSchema(): Promise { log(`Cache is from a previous version (${storedVersion}) of the extension. Updating for version ${__VERSION__}`); needsUpdate = true } else { + log(`Cache is from current version (${storedVersion}) of the extension.`); itemSchema = await getStorageValue(storage_schema, null); } @@ -87,8 +114,8 @@ export async function prepareSchema(): Promise { var responseItems: any[] = await response.json() // We want to keep the keys `defindex`, `item_name`, and `attributes` responseItems.forEach((item: any) => { - const defindex = item['defindex'] - const name = item['item_name'] + const defindex: number = item['defindex'] + var tradable: Boolean = true try { if(item['attributes'] != null) { @@ -100,7 +127,26 @@ export async function prepareSchema(): Promise { logError(error) log(item) } - (cacheItems as any)[defindex.toString()] = { "name": name, "tradable": tradable } + + var canKillstreakify: Boolean = false + try { + if(item['capabilities'] != null) { + if(item['capabilities']['can_killstreakify'] != null && item['capabilities']['can_killstreakify'] == true) { + canKillstreakify = true + } + } + } catch(error) { + logError(error) + log(item) + } + + (cacheItems as any)[defindex.toString()] = { + "name": item['item_name'], + "slot": item['item_slot'], + "tradable": tradable, + "canKillstreakify": canKillstreakify, + "hasAustraliumVariant": checkAustraliumVariant(defindex) + } }); await setStorageValue(storage_schema, (cacheItems));