Skip to content

Commit bf6260d

Browse files
committed
Add no-Steam runtime tests
1 parent 4e3a400 commit bf6260d

1 file changed

Lines changed: 68 additions & 2 deletions

File tree

ts/index.test.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import assert from 'node:assert/strict';
33
import { existsSync } from 'node:fs';
44
import { join } from 'node:path';
55
import { getBin } from '@node-3d/addon-tools';
6+
import type { TSteamId } from './index.ts';
67

78
const nativeBinaryPath = join(import.meta.dirname, '..', getBin(), 'steam-api.node');
9+
const nativeSkip = existsSync(nativeBinaryPath) ? false : 'native binary is not built';
10+
const loadSteamApi = () => import('./index.ts');
811

912
test(
1013
'native addon contract is loadable when a prebuilt binary is present',
11-
{ skip: existsSync(nativeBinaryPath) ? false : 'native binary is not built' },
14+
{ skip: nativeSkip },
1215
async () => {
13-
const steamApi = await import('./index.ts');
16+
const steamApi = await loadSteamApi();
1417
assert.equal(typeof steamApi.steam.initEx, 'function');
1518
assert.equal(typeof steamApi.callbacks.pollCallbacks, 'function');
1619
assert.equal(typeof steamApi.steamId.isAnonymous, 'function');
@@ -126,3 +129,66 @@ test(
126129
assert.equal(typeof steamApi.userStats.getNumberOfCurrentPlayers, 'function');
127130
},
128131
);
132+
133+
test('Steam lifecycle methods are safe without a Steam session', { skip: nativeSkip }, async () => {
134+
const steamApi = await loadSteamApi();
135+
136+
assert.equal(typeof steamApi.steam.isSteamRunning(), 'boolean');
137+
assert.deepEqual(steamApi.callbacks.pollCallbacks(), []);
138+
assert.doesNotThrow(() => steamApi.steam.releaseCurrentThreadMemory());
139+
140+
const result = steamApi.steam.initEx();
141+
try {
142+
assert.equal(typeof result.result, 'number');
143+
assert.equal(typeof result.ok, 'boolean');
144+
assert.equal(typeof result.errorMessage, 'string');
145+
assert.doesNotThrow(() => steamApi.steam.runCallbacks());
146+
assert.ok(Array.isArray(steamApi.callbacks.pollCallbacks()));
147+
} finally {
148+
assert.doesNotThrow(() => steamApi.steam.shutdown());
149+
}
150+
});
151+
152+
test('SteamID helpers operate without a Steam session', { skip: nativeSkip }, async () => {
153+
const steamApi = await loadSteamApi();
154+
const individualSteamId = '76561197960287930' as TSteamId;
155+
const nilSteamId = '0' as TSteamId;
156+
157+
assert.equal(steamApi.steamId.isValid(individualSteamId), true);
158+
assert.equal(steamApi.steamId.getRawSteamId(individualSteamId), individualSteamId);
159+
assert.equal(steamApi.steamId.getAccountId(individualSteamId), 22202);
160+
assert.equal(
161+
steamApi.steamId.getAccountType(individualSteamId),
162+
steamApi.AccountType.Individual,
163+
);
164+
assert.equal(steamApi.steamId.getStaticAccountKey(individualSteamId), '76561193665320634');
165+
assert.equal(steamApi.steamId.isIndividualAccount(individualSteamId), true);
166+
assert.equal(steamApi.steamId.isAnonymous(individualSteamId), false);
167+
assert.equal(steamApi.steamId.isLobby(individualSteamId), false);
168+
169+
assert.equal(steamApi.steamId.isValid(nilSteamId), false);
170+
assert.equal(steamApi.steamId.getRawSteamId(nilSteamId), nilSteamId);
171+
assert.equal(steamApi.steamId.getAccountId(nilSteamId), 0);
172+
assert.equal(steamApi.steamId.getAccountType(nilSteamId), steamApi.AccountType.Invalid);
173+
assert.equal(steamApi.steamId.getStaticAccountKey(nilSteamId), '0');
174+
assert.equal(steamApi.steamId.isIndividualAccount(nilSteamId), false);
175+
assert.equal(steamApi.steamId.isAnonymous(nilSteamId), false);
176+
assert.equal(steamApi.steamId.isLobby(nilSteamId), false);
177+
});
178+
179+
test('SteamID helpers validate uint64 decimal strings', { skip: nativeSkip }, async () => {
180+
const steamApi = await loadSteamApi();
181+
182+
assert.throws(
183+
() => steamApi.steamId.isValid('' as TSteamId),
184+
/steamId must be a non-empty uint64 decimal string/u,
185+
);
186+
assert.throws(
187+
() => steamApi.steamId.isValid('not-a-steamid' as TSteamId),
188+
/steamId must be a uint64 decimal string/u,
189+
);
190+
assert.throws(
191+
() => steamApi.steamId.isValid('18446744073709551616' as TSteamId),
192+
/steamId is outside the uint64 range/u,
193+
);
194+
});

0 commit comments

Comments
 (0)