Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import { beforeEach, describe, expect, it, vi } from 'vitest';
const deferred = <T>(): { promise: Promise<T>; resolve: (value: T) => void } => {
let resolve!: (value: T) => void;
const promise = new Promise<T>((settle) => {
resolve = settle;
});
return { promise, resolve };
};
const dependencies = vi.hoisted(() => ({
listModelInstalls: vi.fn(),
refreshModels: vi.fn(),
refreshStartersIfLoaded: vi.fn(),
}));
vi.mock('./api', () => ({ listModelInstalls: dependencies.listModelInstalls }));
vi.mock('./modelsStore', () => ({ refreshModels: dependencies.refreshModels }));
vi.mock('./startersStore', () => ({ refreshStartersIfLoaded: dependencies.refreshStartersIfLoaded }));
beforeEach(() => {
vi.resetModules();
vi.useFakeTimers();
dependencies.listModelInstalls.mockReset().mockResolvedValue([]);
dependencies.refreshModels.mockReset().mockResolvedValue(undefined);
dependencies.refreshStartersIfLoaded.mockReset();
});
describe('model install event interpretation', () => {
it("prunes a job's transient progress when it settles", async () => {
const store = await import('./installsStore');
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 25, id: 7, total_bytes: 100 });
expect(store.getInstallProgress(7)).toEqual({ bytes: 25, bytesPerSecond: null, totalBytes: 100 });
store.handleModelInstallSocketEvent('model_install_complete', { config: {}, id: 7, source: 'org/model' });
expect(store.getInstallProgress(7)).toBeNull();
});
it('projects download progress immediately and coalesces the REST refresh', async () => {
const store = await import('./installsStore');
store.addInstallJob({ id: 7, source: 'org/model', status: 'waiting' });
store.handleModelInstallSocketEvent('model_install_download_progress', {
bytes: 25,
id: 7,
total_bytes: 100,
});
expect(store.getInstallsSnapshot().jobs[0]?.status).toBe('downloading');
expect(store.getInstallProgress(7)).toEqual({ bytes: 25, bytesPerSecond: null, totalBytes: 100 });
store.handleModelInstallSocketEvent('model_install_started', { id: 7 });
store.handleModelInstallSocketEvent('model_install_download_started', { id: 7 });
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
});
it('projects a paused install event immediately and refetches the job for its failed parts', async () => {
const pausedWithFailedPart = {
download_parts: [{ source: 'https://example.com/a.safetensors', status: 'error' }],
id: 9,
source: 'org/model',
status: 'paused',
};
dependencies.listModelInstalls.mockReset().mockResolvedValue([pausedWithFailedPart]);
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'downloading' });
store.handleModelInstallSocketEvent('model_install_paused', { id: 9, source: 'org/model' });
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'paused' }]);
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
expect(store.getInstallsSnapshot().jobs).toEqual([pausedWithFailedPart]);
});
it('keeps a pause over an in-flight stale list response and trusts the refetch started after it', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
const fresh = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls.mockReset().mockReturnValueOnce(stale.promise).mockReturnValueOnce(fresh.promise);
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'downloading' });
const refresh = store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_paused', { id: 9, source: 'org/model' });
expect(store.getInstallsSnapshot().jobs[0]?.status).toBe('paused');
stale.resolve([{ id: 9, source: 'org/model', status: 'downloading' }]);
await refresh;
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'paused' }]);
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
fresh.resolve([{ id: 9, source: 'org/model', status: 'downloading' }]);
await vi.advanceTimersByTimeAsync(0);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'downloading' }]);
});
it('reruns the refetch after a stale list request that outlives the coalescing window', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
const pausedWithFailedPart = {
download_parts: [{ source: 'https://example.com/a.safetensors', status: 'error' }],
id: 9,
source: 'org/model',
status: 'paused',
};
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockResolvedValueOnce([pausedWithFailedPart]);
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'downloading' });
void store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_paused', { id: 9, source: 'org/model' });
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
stale.resolve([{ id: 9, source: 'org/model', status: 'downloading' }]);
await vi.advanceTimersByTimeAsync(0);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().jobs).toEqual([pausedWithFailedPart]);
});
it.each([
['model_install_complete', 'completed'],
['model_install_error', 'error'],
['model_install_cancelled', 'cancelled'],
] as const)(
'converges on the settled status after a list response delayed across pause, resume, and %s',
async (settleEvent, settledStatus) => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockResolvedValueOnce([{ id: 9, source: 'org/model', status: settledStatus }]);
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'downloading' });
void store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_paused', { id: 9, source: 'org/model' });
store.handleModelInstallSocketEvent('model_install_download_started', { id: 9, source: 'org/model' });
store.handleModelInstallSocketEvent(settleEvent, { config: {}, id: 9, source: 'org/model' });
stale.resolve([{ id: 9, source: 'org/model', status: 'downloading' }]);
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: settledStatus }]);
}
);
it.each([
['model_install_complete', 'completed'],
['model_install_error', 'error'],
['model_install_cancelled', 'cancelled'],
] as const)(
'keeps the %s status over a list response delayed across pause and resume when the refetch fails',
async (settleEvent, settledStatus) => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValueOnce(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'downloading' });
const refresh = store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_paused', { id: 9, source: 'org/model' });
store.handleModelInstallSocketEvent('model_install_download_started', { id: 9, source: 'org/model' });
store.handleModelInstallSocketEvent(settleEvent, { config: {}, id: 9, source: 'org/model' });
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: settledStatus }]);
stale.resolve([{ id: 9, source: 'org/model', status: 'paused' }]);
await refresh;
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: settledStatus }]);
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: settledStatus }]);
}
);
it('keeps a resume action result over a list response that started while the job was paused', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls.mockReset().mockReturnValueOnce(stale.promise);
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'paused' });
const refresh = store.refreshInstalls();
store.replaceInstallJob({ id: 9, source: 'org/model', status: 'waiting' });
stale.resolve([{ id: 9, source: 'org/model', status: 'paused' }]);
await refresh;
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'waiting' }]);
});
it('trusts a cancelled list status over an older download fence when the trailing refresh fails', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValue(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'waiting' });
void store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_download_started', { id: 9, source: 'org/model' });
void store.refreshInstalls();
stale.resolve([{ id: 9, source: 'org/model', status: 'cancelled' }]);
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(3);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'cancelled' }]);
});
it('drops a pruned settled job omitted by an in-flight response when the trailing refresh fails', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValue(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'running' });
void store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_complete', { config: {}, id: 9, source: 'org/model' });
void store.refreshInstalls();
stale.resolve([]);
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(3);
expect(store.getInstallsSnapshot().jobs).toEqual([]);
});
it('keeps a job first seen after an in-flight response started when it settles before that response lands', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValue(new Error('temporary list failure'));
const store = await import('./installsStore');
void store.refreshInstalls();
store.addInstallJob({ id: 5, source: 'org/new', status: 'waiting' });
store.handleModelInstallSocketEvent('model_install_complete', { config: {}, id: 5, source: 'org/new' });
stale.resolve([]);
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 5, source: 'org/new', status: 'completed' }]);
});
it('keeps a completion that lands before a no-op cancel response', async () => {
dependencies.listModelInstalls.mockReset().mockRejectedValue(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'running' });
store.handleModelInstallSocketEvent('model_install_complete', { config: {}, id: 9, source: 'org/model' });
store.projectInstallCancelled(9);
await vi.advanceTimersByTimeAsync(250);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'completed' }]);
});
it('keeps a cancel action over an in-flight active status when the trailing refresh fails', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValue(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'waiting' });
void store.refreshInstalls();
store.projectInstallCancelled(9);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'cancelled' }]);
void store.refreshInstalls();
stale.resolve([{ id: 9, source: 'org/model', status: 'waiting' }]);
await vi.advanceTimersByTimeAsync(0);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'cancelled' }]);
});
it('keeps a cancel action over successful active refreshes until REST reports a terminal status', async () => {
dependencies.listModelInstalls
.mockReset()
.mockResolvedValueOnce([{ id: 9, source: 'org/model', status: 'downloading' }])
.mockResolvedValueOnce([{ id: 9, source: 'org/model', status: 'completed' }]);
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'waiting' });
store.projectInstallCancelled(9);
await store.refreshInstalls();
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'cancelled' }]);
await store.refreshInstalls();
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'completed' }]);
});
it.each([
['cancel action', 'model_install_started'],
['cancel action', 'model_install_download_started'],
['cancel response', 'model_install_started'],
['cancel response', 'model_install_download_started'],
] as const)('keeps a %s over a delayed %s event when the refetch fails', async (cancelSource, event) => {
dependencies.listModelInstalls.mockReset().mockRejectedValue(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'waiting' });
if (cancelSource === 'cancel action') {
store.replaceInstallJob({ id: 9, source: 'org/model', status: 'waiting' });
store.projectInstallCancelled(9);
} else {
store.replaceInstallJob({ id: 9, source: 'org/model', status: 'cancelled' });
}
store.handleModelInstallSocketEvent(event, { id: 9, source: 'org/model' });
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
expect(store.getInstallsSnapshot().error).toBe('temporary list failure');
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'cancelled' }]);
});
it('keeps a completed cancel response over an older in-flight active row when refresh fails', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValue(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'downloading' });
void store.refreshInstalls();
store.replaceInstallJob({ id: 9, source: 'org/model', status: 'completed' });
void store.refreshInstalls();
stale.resolve([{ id: 9, source: 'org/model', status: 'downloading' }]);
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'completed' }]);
});
it('keeps all fields from a restart action over an older in-flight job row', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string; [key: string]: unknown }>>();
dependencies.listModelInstalls.mockReset().mockReturnValueOnce(stale.promise);
const store = await import('./installsStore');
const oldPart = {
bytes: 40,
error: 'old failure',
source: 'https://example.com/weights.safetensors',
status: 'error',
total_bytes: 100,
};
store.addInstallJob({
bytes: 40,
download_parts: [oldPart],
error: 'old failure',
id: 9,
source: 'org/model',
status: 'error',
total_bytes: 100,
});
const refresh = store.refreshInstalls();
const restarted = {
bytes: 0,
download_parts: [],
error: null,
id: 9,
source: 'org/model',
status: 'waiting' as const,
total_bytes: 100,
};
store.replaceInstallJob(restarted);
stale.resolve([
{
bytes: 40,
download_parts: [oldPart],
error: 'old failure',
id: 9,
source: 'org/model',
status: 'error',
total_bytes: 100,
},
]);
await refresh;
expect(store.getInstallsSnapshot().jobs).toEqual([restarted]);
});
it('keeps a newly created job that an in-flight list response omits', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls.mockReset().mockReturnValueOnce(stale.promise);
const store = await import('./installsStore');
const refresh = store.refreshInstalls();
store.addInstallJob({ id: 5, source: 'org/new', status: 'waiting' });
stale.resolve([]);
await refresh;
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 5, source: 'org/new', status: 'waiting' }]);
});
it('keeps an install error that arrived before the late creation response', async () => {
const list = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls.mockReset().mockReturnValueOnce(list.promise);
const store = await import('./installsStore');
store.handleModelInstallSocketEvent('model_install_error', { error: 'boom', id: 5, source: 'org/new' });
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
store.addInstallJob({ id: 5, source: 'org/new', status: 'waiting' });
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 5, source: 'org/new', status: 'error' }]);
list.resolve([{ id: 5, source: 'org/new', status: 'error' }]);
await vi.advanceTimersByTimeAsync(0);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 5, source: 'org/new', status: 'error' }]);
});
it('keeps a locally known paused job when a stale response omits it and the trailing refresh fails', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValueOnce(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'downloading' });
const refresh = store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_paused', { id: 9, source: 'org/model' });
stale.resolve([]);
await refresh;
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'paused' }]);
});
it('does not let late download progress clear a pause fence', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValueOnce(new Error('outage'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'downloading' });
const refresh = store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_paused', { id: 9, source: 'org/model' });
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 50, id: 9, total_bytes: 100 });
stale.resolve([{ id: 9, source: 'org/model', status: 'downloading' }]);
await refresh;
await vi.advanceTimersByTimeAsync(250);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'paused' }]);
});
it('does not let stale progress fence a locally confirmed completion', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValueOnce(new Error('temporary list failure'));
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'waiting' });
const refresh = store.refreshInstalls();
store.replaceInstallJob({ id: 9, source: 'org/model', status: 'completed' });
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 50, id: 9, total_bytes: 100 });
stale.resolve([{ id: 9, source: 'org/model', status: 'waiting' }]);
await refresh;
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'completed' }]);
});
it('keeps progress after reconnect over an older waiting list row', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls.mockReset().mockReturnValueOnce(stale.promise);
const store = await import('./installsStore');
store.addInstallJob({ id: 9, source: 'org/model', status: 'waiting' });
const refresh = store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 50, id: 9, total_bytes: 100 });
stale.resolve([{ id: 9, source: 'org/model', status: 'waiting' }]);
await refresh;
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'downloading' }]);
});
it('keeps progress for an unknown job over an older waiting list row when the refetch fails', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockRejectedValueOnce(new Error('temporary list failure'));
const store = await import('./installsStore');
const refresh = store.refreshInstalls();
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 50, id: 9, total_bytes: 100 });
stale.resolve([{ id: 9, source: 'org/model', status: 'waiting' }]);
await refresh;
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().error).toBe('temporary list failure');
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 9, source: 'org/model', status: 'downloading' }]);
expect(store.getInstallProgress(9)).toEqual({ bytes: 50, bytesPerSecond: null, totalBytes: 100 });
});
it('records completion identity and refreshes catalog capabilities', async () => {
const store = await import('./installsStore');
store.handleModelInstallSocketEvent('model_install_complete', {
config: { name: 'Installed Model' },
id: 8,
source: { repo_id: 'org/model' },
});
expect(store.getInstallOutcomes()[0]).toMatchObject({
error: null,
jobId: 8,
kind: 'completed',
modelName: 'Installed Model',
source: 'org/model',
});
expect(dependencies.refreshModels).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.refreshModels).toHaveBeenCalledTimes(1);
expect(dependencies.refreshStartersIfLoaded).toHaveBeenCalledTimes(1);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
});
it('coalesces a burst of completions into one library refetch', async () => {
const store = await import('./installsStore');
for (const id of [1, 2, 3, 4]) {
store.handleModelInstallSocketEvent('model_install_complete', { config: {}, id, source: 'org/model' });
}
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.refreshModels).toHaveBeenCalledTimes(1);
expect(dependencies.refreshStartersIfLoaded).toHaveBeenCalledTimes(1);
});
it('drops a stale refresh without releasing the replacement account refresh', async () => {
const first = deferred<Array<{ id: number; source: string; status: 'waiting' }>>();
const second = deferred<Array<{ id: number; source: string; status: 'waiting' }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(first.promise)
.mockReturnValueOnce(second.promise)
.mockResolvedValueOnce([{ id: 2, source: 'new/account', status: 'waiting' }]);
const store = await import('./installsStore');
const { accountLifecycle } = await import('@platform/state/accountLifecycle');
const staleRefresh = store.refreshInstalls();
accountLifecycle.invalidate();
expect(store.getInstallsSnapshot()).toEqual({ dismissedJobIds: new Set(), error: null, jobs: [], status: 'idle' });
const currentRefresh = store.refreshInstalls();
first.resolve([{ id: 1, source: 'old/account', status: 'waiting' }]);
await staleRefresh;
expect(store.getInstallsSnapshot()).toEqual({
dismissedJobIds: new Set(),
error: null,
jobs: [],
status: 'loading',
});
expect(store.refreshInstalls()).toBe(currentRefresh);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
second.resolve([{ id: 2, source: 'new/account', status: 'waiting' }]);
await currentRefresh;
await vi.advanceTimersByTimeAsync(0);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(3);
expect(store.getInstallsSnapshot()).toEqual({
dismissedJobIds: new Set(),
error: null,
jobs: [{ id: 2, source: 'new/account', status: 'waiting' }],
status: 'loaded',
});
});
it('re-fetches once more when a refresh lands while another is in flight', async () => {
const stale = deferred<Array<{ id: number; source: string; status: string }>>();
dependencies.listModelInstalls
.mockReset()
.mockReturnValueOnce(stale.promise)
.mockResolvedValueOnce([{ id: 1, source: 'org/model', status: 'completed' }]);
const store = await import('./installsStore');
store.handleModelInstallSocketEvent('model_install_complete', { config: {}, id: 1, source: 'org/model' });
await vi.advanceTimersByTimeAsync(250);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
store.handleModelInstallSocketEvent('model_install_complete', { config: {}, id: 1, source: 'org/model' });
await vi.advanceTimersByTimeAsync(250);
stale.resolve([{ id: 1, source: 'org/model', status: 'running' }]);
await vi.advanceTimersByTimeAsync(0);
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
expect(store.getInstallsSnapshot().jobs).toEqual([{ id: 1, source: 'org/model', status: 'completed' }]);
});
it('retries a failed load on the next ensure instead of sticking in error', async () => {
dependencies.listModelInstalls
.mockReset()
.mockRejectedValueOnce(new Error('outage'))
.mockResolvedValueOnce([{ id: 1, source: 'org/model', status: 'waiting' }]);
const store = await import('./installsStore');
store.ensureInstallsLoaded();
await vi.advanceTimersByTimeAsync(0);
expect(store.getInstallsSnapshot().status).toBe('error');
store.ensureInstallsLoaded();
await vi.advanceTimersByTimeAsync(0);
expect(store.getInstallsSnapshot()).toEqual({
dismissedJobIds: new Set(),
error: null,
jobs: [{ id: 1, source: 'org/model', status: 'waiting' }],
status: 'loaded',
});
expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(2);
});
it('ignores socket events owned by an expired account scope', async () => {
const store = await import('./installsStore');
const { accountLifecycle } = await import('@platform/state/accountLifecycle');
const owner = accountLifecycle.capture();
accountLifecycle.invalidate();
store.handleModelInstallSocketEvent(
'model_install_download_progress',
{ bytes: 25, id: 7, total_bytes: 100 },
owner
);
expect(store.getInstallProgress(7)).toBeNull();
expect(store.getInstallsSnapshot()).toEqual({ dismissedJobIds: new Set(), error: null, jobs: [], status: 'idle' });
});
});
describe('getInstallSourceLabel', () => {
it('passes strings through and extracts structured source fields in order', async () => {
const store = await import('./installsStore');
expect(store.getInstallSourceLabel('https://example.com/model.safetensors')).toBe(
'https://example.com/model.safetensors'
);
expect(store.getInstallSourceLabel({ repo_id: 'owner/repo', url: 'https://x' })).toBe('owner/repo');
expect(store.getInstallSourceLabel({ url: 'https://x' })).toBe('https://x');
expect(store.getInstallSourceLabel({ path: '/models/x' })).toBe('/models/x');
});
it('falls back to a generic label for unrecognized payloads', async () => {
const store = await import('./installsStore');
expect(store.getInstallSourceLabel({ something: 'else' })).toBe('model');
expect(store.getInstallSourceLabel(undefined)).toBe('model');
expect(store.getInstallSourceLabel(42)).toBe('model');
});
});
describe('install queue presentation state', () => {
it('smooths a transfer rate from spaced progress samples', async () => {
const store = await import('./installsStore');
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 0, id: 7, total_bytes: 1000 });
vi.advanceTimersByTime(100);
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 50, id: 7, total_bytes: 1000 });
expect(store.getInstallProgress(7)?.bytesPerSecond).toBeNull();
vi.advanceTimersByTime(900);
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 500, id: 7, total_bytes: 1000 });
expect(store.getInstallProgress(7)?.bytesPerSecond).toBe(500);
vi.advanceTimersByTime(1000);
store.handleModelInstallSocketEvent('model_install_download_progress', { bytes: 600, id: 7, total_bytes: 1000 });
expect(store.getInstallProgress(7)?.bytesPerSecond).toBe(500 * 0.7 + 100 * 0.3);
});
it('keeps dismissed jobs hidden only while the backend still lists them', async () => {
const store = await import('./installsStore');
dependencies.listModelInstalls.mockResolvedValue([
{ id: 1, source: 'a', status: 'completed' },
{ id: 2, source: 'b', status: 'error' },
]);
await store.refreshInstalls();
store.dismissInstallJob(1);
store.dismissInstallJob(2);
expect([...store.getInstallsSnapshot().dismissedJobIds]).toEqual([1, 2]);
dependencies.listModelInstalls.mockResolvedValue([{ id: 2, source: 'b', status: 'error' }]);
await store.refreshInstalls();
expect([...store.getInstallsSnapshot().dismissedJobIds]).toEqual([2]);
});
});