Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

10.5 KB
Raw Blame
/* frontend/public/sw.js against a stand-in worker environment.
 *
 * The one thing the worker must never do is take the build that is already on the device away
 * without having a working one to put there: its cache is what a home-screen app reopened
 * without a network comes back from. So an install that did not get the shell fails, and the
 * previous build's cache is swept only once this build's shell is in its own. */
import { readFileSync } from 'node:fs'
import { beforeEach, describe, expect, it } from 'vitest'

class FakeCache {
  constructor(name) { this.name = name; this.m = new Map() }
  async put(req, res) { this.m.set(String(req.url || req), res) }
  async add(u) { const r = await globalThis.fetch(u); if (!r.ok) throw new Error('add failed ' + u); this.m.set(String(u), r) }
  async match(req) { return this.m.get(String(req.url || req)) }
  keys() { return [...this.m.keys()] }
}
class FakeCaches {
  constructor() { this.c = new Map() }
  async open(n) { if (!this.c.has(n)) this.c.set(n, new FakeCache(n)); return this.c.get(n) }
  async keys() { return [...this.c.keys()] }
  async delete(n) { return this.c.delete(n) }
  async match(req) { for (const c of this.c.values()) { const hit = await c.match(req); if (hit) return hit } return undefined }
}

const CACHE = 'opengym-rt-__BUILD__'   // the placeholder the build rewrites (vite.config.js swStamp)
const handlers = {}
let skipWaiting = 0

// A fresh copy of the worker each time: it registers its listeners at import, so the module
// cache has to be stepped around with a query string.
async function loadSW() {
  for (const k of Object.keys(handlers)) delete handlers[k]
  skipWaiting = 0
  globalThis.self = {
    addEventListener: (type, fn) => { handlers[type] = fn },
    skipWaiting: () => { skipWaiting++ },
    clients: { claim: async () => {}, matchAll: async () => [], openWindow: async () => {} },
    registration: { getNotifications: async () => [], showNotification: async () => {}, pushManager: {} }
  }
  globalThis.location = { origin: 'https://gym.test' }
  globalThis.caches = new FakeCaches()
  await import('../../public/sw.js?load=' + Math.random())
}
// What waitUntil was handed is what the browser judges the install by, so it is awaited (and its
// rejection is the install failing).
const fire = async (type, ev = {}) => {
  const e = { ...ev, waitUntil: p => { e._done = p }, respondWith: p => { e._res = p } }
  handlers[type](e)
  if (e._done) await e._done
  return e
}
const previousBuild = async () => {
  const old = await globalThis.caches.open('opengym-rt-oldbuild')
  await old.put('index.html', { ok: true, body: 'the shell already on this device' })
  return old
}
const shellHtml = '<html><head><script src="./assets/index-new.js"></script></head></html>'

beforeEach(() => { delete globalThis.self })

describe('sw.js install and activate', () => {
  it('a precache that cannot reach the server fails the install and keeps the build on the device', async () => {
    await loadSW()
    const old = await previousBuild()
    globalThis.fetch = async () => { throw new Error('network down') }

    await expect(fire('install')).rejects.toThrow()
    expect(skipWaiting).toBe(0)

    await fire('activate')   // an activate that happens anyway must still leave the old cache alone
    expect(await globalThis.caches.keys()).toContain('opengym-rt-oldbuild')
    expect(await old.match('index.html')).toBeTruthy()
  })

  it('a redirect to a login page is never cached as the shell', async () => {
    await loadSW()
    await previousBuild()
    globalThis.fetch = async () => ({ ok: true, status: 200, redirected: true, text: async () => '<html>sign in</html>' })

    await expect(fire('install')).rejects.toThrow()
    expect(skipWaiting).toBe(0)
    expect((await globalThis.caches.open(CACHE)).keys()).not.toContain('index.html')
    expect(await globalThis.caches.keys()).toContain('opengym-rt-oldbuild')
  })

  it('a server that is restarting fails the install rather than cache its error page', async () => {
    await loadSW()
    await previousBuild()
    globalThis.fetch = async () => ({ ok: false, status: 502, redirected: false, text: async () => 'Bad Gateway' })

    await expect(fire('install')).rejects.toThrow()
    expect(await globalThis.caches.keys()).toContain('opengym-rt-oldbuild')
  })

  it('a chunk that will not cache fails the install, so the build on the device stays', async () => {
    await loadSW()
    const old = await previousBuild()
    globalThis.fetch = async u => (String(u) === 'index.html'
      ? { ok: true, status: 200, redirected: false, text: async () => shellHtml }
      : { ok: false, status: 404 })

    await expect(fire('install')).rejects.toThrow(/index-new\.js/)
    expect(skipWaiting).toBe(0)
    // The shell is written after its code, so a half-filled cache cannot read as this build.
    expect((await globalThis.caches.open(CACHE)).keys()).not.toContain('index.html')

    await fire('activate')
    expect(await globalThis.caches.keys()).toContain('opengym-rt-oldbuild')
    expect(await old.match('index.html')).toBeTruthy()
  })

  it('a chunk answered by a redirect to a login page fails the install too', async () => {
    // An auth proxy in front answers every request with its login page — 200, and `cache.add`
    // would have stored that HTML under the main bundle's URL.
    await loadSW()
    const old = await previousBuild()
    globalThis.fetch = async u => (String(u) === 'index.html'
      ? { ok: true, status: 200, redirected: false, text: async () => shellHtml }
      : { ok: true, status: 200, redirected: true, text: async () => '<html>sign in</html>' })

    await expect(fire('install')).rejects.toThrow(/index-new\.js.*redirected/)
    expect(skipWaiting).toBe(0)
    expect((await globalThis.caches.open(CACHE)).keys()).toEqual([])

    await fire('activate')
    expect(await globalThis.caches.keys()).toContain('opengym-rt-oldbuild')
    expect(await old.match('index.html')).toBeTruthy()
  })

  it('an icon that will not cache is not worth failing an install over', async () => {
    await loadSW()
    await previousBuild()
    const withIcon = '<html><head><script src="./assets/index-new.js"></script>'
      + '<link rel="stylesheet" href="./assets/index-new.css"><link rel="icon" href="./icon-512.png"></head></html>'
    globalThis.fetch = async u => (String(u) === 'index.html'
      ? { ok: true, status: 200, redirected: false, text: async () => withIcon }
      : String(u).endsWith('.png') ? { ok: false, status: 404 } : { ok: true, status: 200, clone: () => ({}) })

    await fire('install')
    expect(skipWaiting).toBe(1)
    await fire('activate')

    expect(await globalThis.caches.keys()).toEqual([CACHE])
    const c = await globalThis.caches.open(CACHE)
    expect(c.keys()).toEqual(expect.arrayContaining(['./assets/index-new.js', './assets/index-new.css', 'index.html']))
  })

  it('an install that got the shell takes over and drops the previous build', async () => {
    await loadSW()
    await previousBuild()
    globalThis.fetch = async u => (String(u) === 'index.html'
      ? { ok: true, status: 200, redirected: false, text: async () => shellHtml }
      : { ok: true, status: 200, clone: () => ({}) })

    await fire('install')
    expect(skipWaiting).toBe(1)
    await fire('activate')

    expect(await globalThis.caches.keys()).toEqual([CACHE])
    expect((await globalThis.caches.open(CACHE)).keys()).toContain('index.html')
  })
})

// The push service rotates a subscription (expiry, a key change) and the worker re-registers it,
// usually with no page open. It sent no device id, so the server's row lost it and the rest-timer
// alert for that account went to every device until a page next booted. The page leaves the id
// in a cache the worker can read (lib/push.js shareDeviceId).
describe('sw.js re-registers a rotated push subscription with the page\'s device id', () => {
  const rotated = {
    endpoint: 'https://push.example/e2',
    options: { applicationServerKey: new Uint8Array([1, 2, 3]).buffer },
    toJSON: () => ({ endpoint: 'https://push.example/e2', keys: { p256dh: 'p2', auth: 'a2' } })
  }
  const rotate = async () => {
    const sent = []
    globalThis.fetch = async (url, init) => { sent.push([String(url), JSON.parse(init.body)]); return { ok: true, status: 200 } }
    await fire('pushsubscriptionchange', { oldSubscription: { endpoint: 'https://push.example/e1', options: rotated.options }, newSubscription: rotated })
    return sent
  }
  // The same cache name and key on both sides, read out of the two files, so the page and the
  // worker cannot drift apart without this failing.
  const constant = (src, name) => src.match(new RegExp('const ' + name + " = '([^']+)'"))?.[1]
  const pageSrc = readFileSync(new URL('./push.js', import.meta.url), 'utf8')
  const swSrc = readFileSync(new URL('../../public/sw.js', import.meta.url), 'utf8')
  const DEVICE_CACHE = constant(pageSrc, 'DEVICE_CACHE')
  const DEVICE_URL = constant(pageSrc, 'DEVICE_URL')

  it('the page and the worker look in the same place', () => {
    expect(DEVICE_CACHE).toBeTruthy()
    expect(DEVICE_URL).toBeTruthy()
    expect(constant(swSrc, 'DEVICE_CACHE')).toBe(DEVICE_CACHE)
    expect(constant(swSrc, 'DEVICE_URL')).toBe(DEVICE_URL)
  })

  it('sends the device id the page left', async () => {
    await loadSW()
    await (await globalThis.caches.open(DEVICE_CACHE)).put(DEVICE_URL, new Response('dev1234abcd'))
    expect(await rotate()).toEqual([['api/push/subscribe', {
      subscription: { endpoint: 'https://push.example/e2', keys: { p256dh: 'p2', auth: 'a2' } },
      deviceId: 'dev1234abcd'
    }]])
  })

  it('with nothing left there, sends the subscription alone, as before', async () => {
    await loadSW()
    const sent = await rotate()
    expect(sent).toHaveLength(1)
    expect(sent[0][1]).toEqual({ subscription: { endpoint: 'https://push.example/e2', keys: { p256dh: 'p2', auth: 'a2' } } })
  })

  it('a new build\'s activate sweeps the old build\'s cache and keeps the device id', async () => {
    await loadSW()
    await previousBuild()
    await (await globalThis.caches.open(DEVICE_CACHE)).put(DEVICE_URL, new Response('dev1234abcd'))
    globalThis.fetch = async u => (String(u) === 'index.html'
      ? { ok: true, status: 200, redirected: false, text: async () => shellHtml }
      : { ok: true, status: 200, clone: () => ({}) })
    await fire('install')
    await fire('activate')
    expect((await globalThis.caches.keys()).sort()).toEqual([CACHE, DEVICE_CACHE].sort())
    expect(await (await (await globalThis.caches.open(DEVICE_CACHE)).match(DEVICE_URL)).text()).toBe('dev1234abcd')
  })
})