Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions __tests__/lib/pwa.server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import fs from 'node:fs'
import path from 'node:path'

jest.mock('node:fs', () => ({
__esModule: true,
default: {
writeFileSync: jest.fn(),
existsSync: jest.fn(),
readFileSync: jest.fn()
},
writeFileSync: jest.fn(),
existsSync: jest.fn(),
readFileSync: jest.fn()
}))

describe('PWA manifest error handling', () => {
let writePwaManifest

beforeEach(() => {
jest.resetModules()
// Set BUILD_MODE so writePwaManifest actually runs
process.env.BUILD_MODE = 'true'
writePwaManifest = require('@/lib/pwa.server').writePwaManifest
})

afterEach(() => {
delete process.env.BUILD_MODE
jest.restoreAllMocks()
})

it('does not throw when filesystem is read-only', () => {
const fsMock = require('node:fs').default || require('node:fs')
fsMock.writeFileSync.mockImplementation(() => {
throw new Error('read-only filesystem')
})

// Capture console.warn
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})

// Should not throw
expect(() =>
writePwaManifest({ siteInfo: { title: 'Test' }, notionConfig: {} })
).not.toThrow()

// Warning should contain failure reason
expect(warnSpy).toHaveBeenCalled()
const warnArg = warnSpy.mock.calls[0].join(' ')
expect(warnArg).toContain('read-only filesystem')

warnSpy.mockRestore()
})

it('sets manifestWritten=true on successful write', () => {
const fsMock = require('node:fs').default || require('node:fs')
fsMock.writeFileSync.mockImplementation(() => {})

// First call should write successfully
expect(() =>
writePwaManifest({ siteInfo: { title: 'Test' }, notionConfig: {} })
).not.toThrow()

// writeFileSync should have been called
expect(fsMock.writeFileSync).toHaveBeenCalled()
})

it('skips write when BUILD_MODE is not true', () => {
delete process.env.BUILD_MODE

const fsMock = require('node:fs').default || require('node:fs')
fsMock.writeFileSync.mockClear()

writePwaManifest({ siteInfo: { title: 'Test' }, notionConfig: {} })

// writeFileSync should not be called when BUILD_MODE !== 'true'
expect(fsMock.writeFileSync).not.toHaveBeenCalled()
})
})
12 changes: 8 additions & 4 deletions lib/pwa.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ let manifestWritten = false
export const writePwaManifest = ({ siteInfo = {}, notionConfig = {} } = {}) => {
if (manifestWritten || process.env.BUILD_MODE !== 'true') return

const manifestPath = path.join(process.cwd(), 'public', 'manifest.json')
const manifest = buildPwaManifest({ siteInfo, notionConfig })
try {
const manifestPath = path.join(process.cwd(), 'public', 'manifest.json')
const manifest = buildPwaManifest({ siteInfo, notionConfig })

fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`)
manifestWritten = true
fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`)
manifestWritten = true
} catch (err) {
console.warn('[PWA] Failed to write manifest.json:', err?.message || err)
}
}
Loading