Skip to content
Merged
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
19 changes: 18 additions & 1 deletion frontend/src/lib/stores/updateCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ describe('checkForUpdate', () => {
latestVersion: 'v2.0.0',
releaseUrl: 'https://github.com/codebude/librislog/releases/tag/v2.0.0',
});
expect(cached.appVersion).toBe('v1.0.0');
expect(typeof cached.timestamp).toBe('number');
});

it('reads from localStorage cache within TTL and does not fetch again', async () => {
const cachedData = {
timestamp: Date.now(),
data: { latestVersion: 'v2.0.0', releaseUrl: 'https://github.com/codebude/librislog/releases/tag/v2.0.0' },
appVersion: 'v1.0.0',
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(cachedData));
const fetchMock = mockFetch({ version: 'v3.0.0' });
Expand All @@ -149,6 +151,7 @@ describe('checkForUpdate', () => {
const cachedData = {
timestamp: Date.now() - 61 * 60 * 1000,
data: { latestVersion: 'v2.0.0', releaseUrl: '' },
appVersion: 'v1.0.0',
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(cachedData));
const fetchMock = mockFetch({ version: 'v3.0.0' });
Expand All @@ -170,14 +173,28 @@ describe('checkForUpdate', () => {
await checkForUpdate();
const cached = JSON.parse(localStorage.getItem(STORAGE_KEY)!);
expect(cached.data).toBeNull();
expect(cached.appVersion).toBe('v1.0.0');
});

it('skips fetch when cached null result is still valid', async () => {
const cachedData = { timestamp: Date.now(), data: null };
const cachedData = { timestamp: Date.now(), data: null, appVersion: 'v1.0.0' };
localStorage.setItem(STORAGE_KEY, JSON.stringify(cachedData));
const fetchMock = mockFetch({ version: 'v2.0.0' });
const result = await checkForUpdate();
expect(fetchMock).not.toHaveBeenCalled();
expect(result).toBeNull();
});

it('refetches when app version changed', async () => {
const cachedData = {
timestamp: Date.now(),
data: { latestVersion: 'v2.0.0', releaseUrl: '' },
appVersion: 'v0.9.0',
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(cachedData));
const fetchMock = mockFetch({ version: 'v1.0.0' });
const result = await checkForUpdate();
expect(fetchMock).toHaveBeenCalledOnce();
expect(result).toBeNull();
});
});
13 changes: 8 additions & 5 deletions frontend/src/lib/stores/updateCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function isNewer(latest: string, current: string): boolean {
interface CachedData {
timestamp: number;
data: UpdateInfo | null;
appVersion: string;
}

export async function checkForUpdate(): Promise<UpdateInfo | null> {
Expand All @@ -44,7 +45,9 @@ export async function checkForUpdate(): Promise<UpdateInfo | null> {
if (cached) {
try {
const parsed: CachedData = JSON.parse(cached);
if (Date.now() - parsed.timestamp < CACHE_TTL) {
if (parsed.appVersion !== version) {
localStorage.removeItem(STORAGE_KEY);
} else if (Date.now() - parsed.timestamp < CACHE_TTL) {
return parsed.data;
}
} catch {
Expand All @@ -55,22 +58,22 @@ export async function checkForUpdate(): Promise<UpdateInfo | null> {
try {
const res = await fetch(CHECK_URL);
if (!res.ok) {
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), data: null }));
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), data: null, appVersion: version }));
return null;
}
const data: { version?: string; release_url?: string } = await res.json();
if (!data.version || !isNewer(data.version, version)) {
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), data: null }));
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), data: null, appVersion: version }));
return null;
}
const info: UpdateInfo = {
latestVersion: data.version,
releaseUrl: data.release_url ?? `https://github.com/codebude/librislog/releases/tag/${data.version}`,
};
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), data: info }));
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), data: info, appVersion: version }));
return info;
} catch {
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), data: null }));
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), data: null, appVersion: version }));
return null;
}
}