You've already forked tf2wikipricing
120 lines
4.0 KiB
TypeScript
120 lines
4.0 KiB
TypeScript
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',
|
|
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)
|
|
expect(getItemIndexByName(mockSchema, 'Non-Existent Item')).toBeNull()
|
|
})
|
|
|
|
test('getTradableStatusByDefindex returns correct status', () => {
|
|
expect(getTradableStatusByDefindex(mockSchema, 208)).toBe(true)
|
|
expect(getTradableStatusByDefindex(mockSchema, 5021)).toBe(true)
|
|
expect(getTradableStatusByDefindex(mockSchema, 69420)).toBe(false)
|
|
expect(() => getTradableStatusByDefindex(mockSchema, 999)).toThrow()
|
|
})
|
|
|
|
test('getTradableStatusByName returns correct status', () => {
|
|
expect(getTradableStatusByName(mockSchema, 'Flame Thrower')).toBe(true)
|
|
expect(getTradableStatusByName(mockSchema, 'Mann Co. Supply Crate Key')).toBe(true)
|
|
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
|
|
}
|
|
});
|
|
});
|
|
}) |