steam.service.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  2. import { EventEmitter } from 'events'
  3. import { encodeBigInts } from '../steam-host-protocol'
  4. class FakeHost extends EventEmitter {
  5. requests: { id: number; method: string }[] = []
  6. kill = vi.fn<() => void>(() => {
  7. this.emit('exit', null)
  8. })
  9. postMessage(message: { id: number; method: string }): void {
  10. this.requests.push(message)
  11. }
  12. answer(id: number, result: unknown): void {
  13. this.emit('message', encodeBigInts({ id, result }))
  14. }
  15. }
  16. const hosts: FakeHost[] = []
  17. vi.mock('electron', () => ({
  18. app: { on: vi.fn<() => void>() },
  19. utilityProcess: {
  20. fork: () => {
  21. const host = new FakeHost()
  22. hosts.push(host)
  23. return host
  24. }
  25. }
  26. }))
  27. vi.mock('./config.service', () => ({ getSteamIdentity: () => 'wallpaper-engine' }))
  28. vi.mock('electron-store', () => ({
  29. default: class {
  30. private data: Record<string, unknown>
  31. constructor(opts: { defaults: Record<string, unknown> }) {
  32. this.data = structuredClone(opts.defaults)
  33. }
  34. get(key: string): unknown {
  35. return this.data[key]
  36. }
  37. set(key: string, value: unknown): void {
  38. this.data[key] = value
  39. }
  40. }
  41. }))
  42. vi.mock('fs', async (importOriginal) => {
  43. const actual = await importOriginal<typeof import('fs')>()
  44. return {
  45. ...actual,
  46. readFileSync: (file: string, ...rest: unknown[]) => {
  47. if (String(file).endsWith('steam.pid')) return '4242'
  48. if (file === '/proc/4242/comm') return 'steam\n'
  49. return (actual.readFileSync as (...args: unknown[]) => unknown)(file, ...rest)
  50. }
  51. }
  52. })
  53. const steam = await import('./steam.service')
  54. const window = {
  55. isDestroyed: () => false,
  56. isVisible: () => true,
  57. isMinimized: () => false,
  58. on: () => {}
  59. } as never
  60. async function untilRequest(host: FakeHost, method: string): Promise<{ id: number }> {
  61. await vi.waitFor(() => { expect(host.requests.some((r) => r.method === method)).toBe(true) })
  62. return host.requests.find((r) => r.method === method)!
  63. }
  64. describe('a Steam request that never answers', () => {
  65. beforeEach(() => {
  66. vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] })
  67. steam.bindSteamToWindow(window)
  68. })
  69. afterEach(() => {
  70. vi.useRealTimers()
  71. })
  72. it('drops the host that did not answer and starts a new one for the next request', async () => {
  73. const first = steam.getLocalAccountId()
  74. await vi.waitFor(() => { expect(hosts).toHaveLength(1) })
  75. hosts[0].answer((await untilRequest(hosts[0], 'init')).id, null)
  76. await untilRequest(hosts[0], 'workshop.getSubscribedItems').catch(() => undefined)
  77. await untilRequest(hosts[0], 'localplayer.getSteamId')
  78. const failure = first.then(() => new Error('answered'), (err: Error) => err)
  79. await vi.advanceTimersByTimeAsync(60_000)
  80. expect((await failure).message).toMatch(/timed out/)
  81. expect(hosts[0].kill).toHaveBeenCalledTimes(1)
  82. const second = steam.getLocalAccountId()
  83. await vi.waitFor(() => { expect(hosts).toHaveLength(2) })
  84. hosts[1].answer((await untilRequest(hosts[1], 'init')).id, null)
  85. const request = await untilRequest(hosts[1], 'localplayer.getSteamId')
  86. hosts[1].answer(request.id, { accountId: 7 })
  87. expect(await second).toBe(7)
  88. })
  89. })