-
Notifications
You must be signed in to change notification settings - Fork 7
fix(pwa): never delete a CacheStorage entry this app doesn't own #513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // QNBS-v3: proves the Tauri-teardown cache cleanup in register-sw.ts never deletes an unowned cache. | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('../../services/logger', () => ({ | ||
| logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, | ||
| })); | ||
|
|
||
| import { isWorldScriptOwnedCacheName, registerServiceWorker } from '../../register-sw'; | ||
|
|
||
| const CURRENT_STATIC = 'worldscript-static-v1.28.1'; | ||
| const FOREIGN_CACHE = 'some-other-github-pages-app-cache-v1'; | ||
| const COLLIDING_FOREIGN_CACHE = 'worldscript-static-vendor-cache'; | ||
|
|
||
| describe('register-sw — isWorldScriptOwnedCacheName', () => { | ||
| it('accepts real owned cache names', () => { | ||
| expect(isWorldScriptOwnedCacheName(CURRENT_STATIC)).toBe(true); | ||
| expect(isWorldScriptOwnedCacheName('worldscript-dynamic-v1.28.1')).toBe(true); | ||
| expect(isWorldScriptOwnedCacheName('worldscript-images-v1.28.1')).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects a foreign cache whose name merely shares the owned prefix', () => { | ||
| expect(isWorldScriptOwnedCacheName(COLLIDING_FOREIGN_CACHE)).toBe(false); | ||
| expect(isWorldScriptOwnedCacheName(FOREIGN_CACHE)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('register-sw — Tauri teardown never deletes an unowned cache', () => { | ||
| let deletedNames: string[]; | ||
| let cacheStore: Set<string>; | ||
|
|
||
| beforeEach(() => { | ||
| deletedNames = []; | ||
| cacheStore = new Set([CURRENT_STATIC, FOREIGN_CACHE, COLLIDING_FOREIGN_CACHE]); | ||
|
|
||
| Object.defineProperty(window, '__TAURI_INTERNALS__', { | ||
| value: {}, | ||
| writable: true, | ||
| configurable: true, | ||
| enumerable: true, | ||
| }); | ||
|
|
||
| Object.defineProperty(navigator, 'serviceWorker', { | ||
| value: { | ||
| getRegistrations: async () => [], | ||
| }, | ||
| writable: true, | ||
| configurable: true, | ||
| enumerable: true, | ||
| }); | ||
|
|
||
| Object.defineProperty(globalThis, 'caches', { | ||
| value: { | ||
| keys: async () => [...cacheStore], | ||
| delete: async (name: string) => { | ||
| deletedNames.push(name); | ||
| return cacheStore.delete(name); | ||
| }, | ||
| }, | ||
| writable: true, | ||
| configurable: true, | ||
| enumerable: true, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // @ts-expect-error — test-only cleanup of a property this suite defines itself. | ||
| delete window.__TAURI_INTERNALS__; | ||
| // @ts-expect-error — jsdom's Navigator normally lacks serviceWorker; restore that absence. | ||
| delete navigator.serviceWorker; | ||
| // @ts-expect-error — jsdom lacks a global caches object by default; restore that absence. | ||
| delete globalThis.caches; | ||
| }); | ||
|
|
||
| it('deletes the owned cache but never the foreign caches, including the colliding-prefix one', async () => { | ||
| await registerServiceWorker(); | ||
| expect(deletedNames).toContain(CURRENT_STATIC); | ||
| expect(deletedNames).not.toContain(FOREIGN_CACHE); | ||
| expect(deletedNames).not.toContain(COLLIDING_FOREIGN_CACHE); | ||
| expect(cacheStore.has(FOREIGN_CACHE)).toBe(true); | ||
| expect(cacheStore.has(COLLIDING_FOREIGN_CACHE)).toBe(true); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.