AI media harness for planning, generating, and editing locally
TypeScript
// @vitest-environment jsdom
import { invoke } from "@tauri-apps/api/core";
import { cancelWeightDownload, downloadLora, prepareLora, removeLora, refreshDownloadedLoras, retryWeightDownload } from "./weightDownloads";
import { HYPERFLOW_LORA, PIKA_FL2VA_LORA, PIKA_REF2VA_LORA, DMAD_LORA, LIGHTX2V_TURBO_LORA, loadLoras, saveLoras, TAOMATE_LORA, TURBO_LORA, VIGGLE_ANIMATE_LORA } from "./loras";
import { beforeEach, expect, it, vi } from "vitest";
import { chooseWeightSource, downloadTemplateWeights, getWeightDownloadState, refreshDownloadedWeights, removeTemplateWeights, updateWeightPath, weightDownloadProgress, type DownloadState } from "./weightDownloads";
import { ANIMATE_CONDITIONING_URL, minimaxImageTemplate, mossImageTemplate, fiboImageTemplate, engineProviderSetting, createGeneratorTemplate, defaultGeneratorTemplate, isDownloadUrl, loadGeneratorTemplateSettings, minimaxOriginalTemplate, minimaxSingularityTemplate, saveGeneratorTemplateSettings, templateNeedsDownload, type WeightSource } from "./settings";
vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn(async () => () => undefined) }));
vi.mock("./persistence", () => ({ isTauri: () => true }));
vi.mock("./nativeShell", () => ({ askNative: vi.fn(async () => true) }));
const files = new Set<string>();
const source = (url: string, gpuModel = "", minVramGb = 0): WeightSource => ({ url, gpuModel, minVramGb });
const saved = () => loadGeneratorTemplateSettings().templates.find((template) => template.id === "minimax-h3-original")!;
it("downloads the three compatible H3 Image weights and makes the template ready", async () => {
const template = minimaxImageTemplate();
await downloadTemplateWeights(template.id);
const urls = vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight").map(([, args]) => (args as { url: string }).url);
expect(urls.sort()).toEqual([template.paths.transformer, minimaxOriginalTemplate().paths.textEncoder, template.paths.videoVae].sort());
const downloaded = loadGeneratorTemplateSettings().templates.find(({ id }) => id === template.id)!;
expect(templateNeedsDownload(downloaded)).toBe(false);
expect(downloaded.paths.audioVae).toBe("");
expect(downloaded.paths.tokenizer).toBe("");
});
it("downloads only Fibo's three image weights and marks the generator ready", async () => {
const fibo = fiboImageTemplate();
await downloadTemplateWeights(fibo.id);
const urls = vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight").map(([, args]) => (args as { url: string }).url);
expect(urls.sort()).toEqual(Object.values(fibo.paths).filter(Boolean).sort());
expect(urls).toHaveLength(3);
const downloaded = loadGeneratorTemplateSettings().templates.find(({ id }) => id === fibo.id)!;
expect(templateNeedsDownload(downloaded)).toBe(false);
expect(downloaded.paths.tokenizer).toBe("");
expect(downloaded.paths.audioVae).toBe("");
});
it("downloads the MOSS weights and tokenizer without audio or adapters", async () => {
const moss = mossImageTemplate();
await downloadTemplateWeights(moss.id);
const urls = vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight").map(([, args]) => (args as { url: string }).url);
expect(urls.sort()).toEqual(Object.values(moss.paths).filter(Boolean).sort());
const downloaded = loadGeneratorTemplateSettings().templates.find(({ id }) => id === moss.id)!;
expect(templateNeedsDownload(downloaded)).toBe(false);
expect(downloaded.paths.tokenizer).toBe("C:/Slopus/weights/tokenizer.json");
expect(downloaded.paths.audioVae).toBe("");
});
it("prepares downloaded adapters before publishing them and does not offer fake cancellation", async () => {
const normal = vi.mocked(invoke).getMockImplementation()!;
let finish!: () => void;
vi.mocked(invoke).mockImplementation(async (command, args) => command === "prepare_lora"
? new Promise<void>((resolve) => { finish = resolve; }) : normal(command, args));
const pending = downloadLora(TURBO_LORA.id);
await vi.waitFor(() => expect(getWeightDownloadState()?.phase).toBe("preparing"));
expect(loadLoras().find(({ id }) => id === TURBO_LORA.id)).toMatchObject({ path: "", needsPreparation: true });
await cancelWeightDownload();
expect(invoke).not.toHaveBeenCalledWith("cancel_weight_download", expect.anything());
expect(invoke).toHaveBeenCalledWith("prepare_lora", { path: expect.stringContaining(".safetensors"), allowDownload: true });
expect(getWeightDownloadState()?.active).toBe(true);
finish(); await pending;
expect(loadLoras().find(({ id }) => id === TURBO_LORA.id)?.needsPreparation).toBeUndefined();
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 1, error: null });
});
it("keeps failed preparation unavailable across discovery and retries it", async () => {
const normal = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "prepare_lora") throw new Error("Grid missing");
if (command === "find_downloaded_weights") return { [TURBO_LORA.url!]: "C:/raw-adapter.safetensors" };
return normal(command, args);
});
await downloadLora(TURBO_LORA.id);
await refreshDownloadedLoras();
expect(loadLoras().find(({ id }) => id === TURBO_LORA.id)).toMatchObject({ path: "", needsPreparation: true });
expect(getWeightDownloadState()).toMatchObject({ active: false, phase: "preparing", error: "Grid missing" });
expect(() => engineProviderSetting(createGeneratorTemplate().paths, undefined, "sage2", [{ loraId: TURBO_LORA.id, enabled: true, strength: 1 }])).toThrow(/Prepare LoRA/);
vi.mocked(invoke).mockImplementation(normal);
await retryWeightDownload();
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 1, error: null });
expect(loadLoras().find(({ id }) => id === TURBO_LORA.id)?.path).not.toBe("");
});
it("repairs local adapters with local-only assets and preserves that choice on retry", async () => {
const lora = { id: "local", name: "Style", path: "D:/style.safetensors" };
saveLoras([lora]);
vi.mocked(invoke).mockRejectedValueOnce(new Error("Companion missing"));
await expect(prepareLora(lora, false)).rejects.toThrow("Companion missing");
expect(loadLoras().find(({ id }) => id === lora.id)?.needsPreparation).toBe(true);
await retryWeightDownload();
expect(invoke).toHaveBeenLastCalledWith("prepare_lora", { path: lora.path, allowDownload: false });
expect(loadLoras().find(({ id }) => id === lora.id)).toMatchObject(lora);
expect(loadLoras().find(({ id }) => id === lora.id)?.needsPreparation).toBeUndefined();
expect(invoke).not.toHaveBeenCalledWith("download_weight", expect.anything());
});
it("downloads Animate with its transformer and four-step distillation adapter", async () => {
await downloadTemplateWeights("viggle-animate");
const template = loadGeneratorTemplateSettings().templates.find(({ id }) => id === "viggle-animate")!;
expect(templateNeedsDownload(template)).toBe(false);
expect(vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight")
.some(([, args]) => String((args as { url?: string }).url).includes("qwen"))).toBe(false);
expect(template.paths.transformer).toBe("C:/Slopus/weights/Viggle-Animate-pruned_rank8_int8_convrot.safetensors");
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({ url: ANIMATE_CONDITIONING_URL }));
expect(template.additionalSafetensors![0].downloadedPath).toBe("C:/Slopus/weights/fixed_embed_fwd_anyframe.safetensors");
expect(engineProviderSetting(template.paths, undefined, template.attention, template.loras, template.mode, template.additionalSafetensors).options.promptEmbedding)
.toBe("C:/Slopus/weights/fixed_embed_fwd_anyframe.safetensors");
expect(getWeightDownloadState()).toMatchObject({ completed: 5, files: 5, active: false, error: null });
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({ url: VIGGLE_ANIMATE_LORA.url }));
expect(loadLoras().find(({ id }) => id === VIGGLE_ANIMATE_LORA.id)).toMatchObject({
path: "C:/Slopus/weights/viggle_animate_distillation_bf16.safetensors", stepOverride: 4,
});
});
it("discovers moved additional safetensors and makes missing files downloadable again", async () => {
await downloadTemplateWeights("viggle-animate");
const animate = () => loadGeneratorTemplateSettings().templates.find(({ id }) => id === "viggle-animate")!;
const oldPath = animate().additionalSafetensors![0].downloadedPath!;
files.delete(oldPath);
const normal = vi.mocked(invoke).getMockImplementation()!;
const moved = "D:/weights/conditioning.safetensors";
files.add(moved);
vi.mocked(invoke).mockImplementation(async (command, args) => command === "find_downloaded_weights"
? { [ANIMATE_CONDITIONING_URL]: moved } : normal(command, args));
await refreshDownloadedWeights();
expect(animate().additionalSafetensors![0].downloadedPath).toBe(moved);
expect(templateNeedsDownload(animate())).toBe(false);
vi.mocked(invoke).mockImplementation(normal);
files.delete(moved);
await refreshDownloadedWeights();
expect(animate().additionalSafetensors![0].downloadedPath).toBeUndefined();
expect(templateNeedsDownload(animate())).toBe(true);
await downloadTemplateWeights("viggle-animate");
expect(getWeightDownloadState()).toMatchObject({ files: 1, completed: 1, error: null });
await removeTemplateWeights("viggle-animate");
expect(invoke).toHaveBeenCalledWith("remove_downloaded_weights", { paths: expect.arrayContaining([oldPath]) });
expect(animate().additionalSafetensors![0]).toMatchObject({ url: ANIMATE_CONDITIONING_URL, role: "promptEmbedding" });
expect(animate().additionalSafetensors![0].downloadedPath).toBeUndefined();
});
it("preserves additional file edits while a download is in flight and retries failures", async () => {
const template = createGeneratorTemplate("Extra files");
template.additionalSafetensors = [{ id: "extra", name: "Extra", url: "https://example.com/extra.safetensors" }];
saveGeneratorTemplateSettings({ templates: [template], defaultTemplateId: "", catalogVersion: 8 });
const normal = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "download_weight") throw new Error("Network lost");
return normal(command, args);
});
await downloadTemplateWeights(template.id);
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 0, error: "Network lost" });
expect(templateNeedsDownload(loadGeneratorTemplateSettings().templates[0])).toBe(true);
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "download_weight") {
const current = loadGeneratorTemplateSettings();
current.templates[0].additionalSafetensors![0].url = "https://example.com/replacement.safetensors";
saveGeneratorTemplateSettings(current);
}
return normal(command, args);
});
await retryWeightDownload();
const extra = loadGeneratorTemplateSettings().templates[0].additionalSafetensors![0];
expect(extra.url).toBe("https://example.com/replacement.safetensors");
expect(extra.downloadedPath).toBeUndefined();
vi.mocked(invoke).mockImplementation(normal);
await retryWeightDownload();
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 1, error: null });
});
it("downloads globally enabled LoRAs with a generator without baking them into its selection", async () => {
const template = createGeneratorTemplate();
saveGeneratorTemplateSettings({ templates: [template], defaultTemplateId: template.id, catalogVersion: 17 });
saveLoras([{ ...DMAD_LORA, globallyEnabled: true }]);
expect(templateNeedsDownload(loadGeneratorTemplateSettings().templates[0])).toBe(true);
await downloadTemplateWeights(template.id);
expect(invoke).toHaveBeenCalledWith("download_weight", { requestId: expect.any(String), url: DMAD_LORA.url });
expect(loadLoras().find((lora) => lora.id === DMAD_LORA.id)).toMatchObject({ globallyEnabled: true, path: expect.stringContaining("dmad") });
expect(loadGeneratorTemplateSettings().templates[0].loras ?? []).toEqual([]);
});
it.each([TAOMATE_LORA, TURBO_LORA, VIGGLE_ANIMATE_LORA, LIGHTX2V_TURBO_LORA, DMAD_LORA, PIKA_FL2VA_LORA, PIKA_REF2VA_LORA, HYPERFLOW_LORA])("downloads $name through the weight transfer and restores missing downloads", async (lora) => {
const savedLora = () => loadLoras().find(({ id }) => id === lora.id)!;
await downloadLora(lora.id);
expect(invoke).toHaveBeenCalledWith("download_weight", { requestId: expect.any(String), url: lora.url });
const path = `C:/Slopus/weights/${lora.url!.split('/').at(-1)}`;
expect(savedLora()).toMatchObject({ ...lora, path });
const options = engineProviderSetting(createGeneratorTemplate().paths, undefined, "sage2", [{ loraId: lora.id, enabled: true, strength: 1 }]).options;
expect(options.stepOverride).toBe(lora.samplingPreset === "dmad-4step" ? 4 : lora.samplingPreset === "pika-6step" ? 6 : lora.stepOverride);
expect(options.samplingPreset).toBe(lora.samplingPreset);
expect(JSON.parse(options.loras as string)).toEqual([{ path, strength: 1 }]);
expect(getWeightDownloadState()).toMatchObject({ loraId: lora.id, active: false, completed: 1, error: null });
files.clear();
await refreshDownloadedLoras();
expect(savedLora().path).toBe("");
await downloadLora(lora.id);
await removeLora(lora.id);
expect(invoke).toHaveBeenCalledWith("remove_downloaded_weights", { paths: [path] });
expect(savedLora().path).toBe("");
});
it("removes manually added adapters without deleting the user's file", async () => {
saveLoras([{ id: "manual", name: "Manual", path: "D:/personal.safetensors" }]);
await removeLora("manual");
expect(loadLoras().some(({ id }) => id === "manual")).toBe(false);
expect(invoke).not.toHaveBeenCalledWith("remove_downloaded_weights", expect.anything());
});
it("retries a failed LoRA download without leaving the shared transfer locked", async () => {
const normal = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockRejectedValueOnce(new Error("Network lost"));
await downloadLora(TAOMATE_LORA.id);
expect(getWeightDownloadState()).toMatchObject({ active: false, error: "Network lost" });
expect(loadLoras()[0].path).toBe("");
vi.mocked(invoke).mockImplementation(normal);
await retryWeightDownload();
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 1, error: null });
});
it("keeps overall progress continuous across files and below 100 until completion", () => {
const download: DownloadState = { templateId: "sample", field: "transformer", downloaded: 100, total: 100, completed: 0, files: 4, active: true, error: null };
expect(weightDownloadProgress(download)).toBe(25);
expect(weightDownloadProgress({ ...download, completed: 1, field: null, downloaded: 0, total: null })).toBe(25);
expect(weightDownloadProgress({ ...download, completed: 1, downloaded: 50 })).toBe(37.5);
expect(weightDownloadProgress({ ...download, completed: 1, total: null })).toBe(25);
expect(weightDownloadProgress({ ...download, files: 0 })).toBe(0);
expect(weightDownloadProgress({ ...download, completed: 3 })).toBe(99);
expect(weightDownloadProgress({ ...download, completed: 4, field: null, active: false })).toBe(100);
});
beforeEach(() => {
localStorage.clear(); files.clear(); vi.clearAllMocks();
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "weight_download_hardware") return [{ name: "NVIDIA GeForce RTX 5090", memoryBytes: 32 * 1024 ** 3 }];
if (command === "find_downloaded_weights") return {};
if (command === "check_weight_files") return Object.fromEntries((args as { paths: string[] }).paths.map((path) => [path, files.has(path)]));
if (command === "remove_downloaded_weights") { for (const path of (args as { paths: string[] }).paths) files.delete(path); return; }
if (command === "download_weight") {
const path = `C:/Slopus/weights/${(args as { url: string }).url.split('/').at(-1)}`;
files.add(path); return path;
}
});
});
it("discovers model weights and LoRAs without saved paths or starting downloads", async () => {
const normal = vi.mocked(invoke).getMockImplementation()!;
const template = minimaxSingularityTemplate();
const cached = Object.fromEntries([...Object.values(template.paths).filter(Boolean), TURBO_LORA.url!]
.map((url) => [url, `D:/Projects/weights/${url.split('/').at(-1)}`]));
vi.mocked(invoke).mockImplementation(async (command, args) => command === "find_downloaded_weights"
? Object.fromEntries((args as { urls: string[] }).urls.filter((url) => cached[url]).map((url) => [url, cached[url]])) : normal(command, args));
await refreshDownloadedWeights();
const discovered = loadGeneratorTemplateSettings().templates.find(({ id }) => id === template.id)!;
expect(discovered.paths.transformer).toBe(cached[template.paths.transformer]);
expect(discovered.sources!.transformer![0].downloadedPath).toBe(cached[template.paths.transformer]);
expect(loadLoras().find(({ id }) => id === TURBO_LORA.id)?.path).toBe(cached[TURBO_LORA.url!]);
expect(templateNeedsDownload(discovered)).toBe(false);
expect(invoke).not.toHaveBeenCalledWith("download_weight", expect.anything());
});
it("finds moved weights and preserves manual paths and GPU variant selection", async () => {
const template = minimaxOriginalTemplate();
const movedUrl = template.paths.videoVae;
template.paths.videoVae = "C:/old/video.safetensors";
template.sources!.videoVae![0].downloadedPath = template.paths.videoVae;
template.paths.audioVae = "D:/personal/audio.safetensors";
files.add(template.paths.audioVae);
saveGeneratorTemplateSettings({ templates: [template], defaultTemplateId: "", catalogVersion: 6 });
const normal = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "find_downloaded_weights") return {
[movedUrl]: "D:/Projects/weights/video.safetensors",
[template.sources!.audioVae![0].url]: "D:/Projects/weights/audio.safetensors",
[template.sources!.transformer![1].url]: "D:/Projects/weights/small.safetensors",
};
return normal(command, args);
});
await refreshDownloadedWeights();
expect(saved().paths.videoVae).toBe("D:/Projects/weights/video.safetensors");
expect(saved().paths.audioVae).toBe("D:/personal/audio.safetensors");
expect(saved().paths.transformer).toBe(template.paths.transformer);
expect(saved().sources!.transformer![1].downloadedPath).toBe("D:/Projects/weights/small.safetensors");
});
it("chooses a matching GPU variant and the largest VRAM tier that fits", () => {
const generic = source("https://example.com/small"), large = source("https://example.com/large", "", 24);
const specific = source("https://example.com/5090", "RTX 5090", 32);
const oversized = source("https://example.com/huge", "RTX 5090", 48);
const variants = [generic, large, specific, oversized];
expect(chooseWeightSource(variants, [{ name: "NVIDIA GeForce RTX 5090", memoryBytes: 32 * 1024 ** 3 }])).toBe(specific);
expect(chooseWeightSource(variants, [{ name: "NVIDIA GeForce RTX 5090", memoryBytes: 33753661440 }])).toBe(specific);
expect(chooseWeightSource(variants, [{ name: "NVIDIA GeForce RTX 4090", memoryBytes: 24 * 1024 ** 3 }])).toBe(large);
expect(chooseWeightSource(variants, [{ name: "AMD Radeon RX 7600", memoryBytes: 8 * 1024 ** 3 }])).toBe(generic);
expect(chooseWeightSource(variants, [])).toBe(generic);
expect(chooseWeightSource([specific], [{ name: "RTX 3090", memoryBytes: 24 * 1024 ** 3 }])).toBeNull();
});
it("preserves the sample links and prevents an unfinished template from being the default", () => {
const sample = minimaxOriginalTemplate();
saveGeneratorTemplateSettings({ templates: [sample], defaultTemplateId: sample.id, catalogVersion: 1 });
expect(templateNeedsDownload(saved())).toBe(true);
expect(loadGeneratorTemplateSettings().defaultTemplateId).toBe("");
expect(defaultGeneratorTemplate().id).toBe("");
expect(Object.values(saved().sources ?? {}).flat()).toHaveLength(6);
});
it.each([8, 11.4, 12, 12.01, 16, 19.4, 20, 20.01, 24, 32])("selects the Minimax weights for %s GiB of VRAM", async (vram) => {
const original = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => command === "weight_download_hardware"
? [{ name: "GPU", memoryBytes: vram * 1024 ** 3 }] : original(command, args));
await downloadTemplateWeights("minimax-h3-original");
const filename = "qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors";
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 4, error: null });
expect(saved().paths.textEncoder).toBe(`C:/Slopus/weights/${filename}`);
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({
url: minimaxOriginalTemplate().paths.textEncoder,
}));
const transformer = vram > 15 && vram <= 20 ? "minimax_h3_fl2va_pruned_w6a8.safetensors" : vram <= 20 ? "minimax_h3_fl2va_pruned-w4a8_convrot_pruned.safetensors" : "minimax_h3_fl2va_pruned_int8_convrot.safetensors";
expect(saved().paths.transformer).toBe(`C:/Slopus/weights/${transformer}`);
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({
url: vram > 15 && vram <= 20 ? "https://huggingface.co/Comfy-Org/MiniMax-H3/resolve/main/diffusion_models/minimax_h3_fl2va_pruned_w6a8.safetensors" : vram <= 20
? "https://huggingface.co/Winnougan/MiniMax-H3-INT4_Convrot_ComfyUI/resolve/main/minimax_h3_fl2va_pruned-w4a8_convrot_pruned.safetensors"
: minimaxOriginalTemplate().paths.transformer,
}));
});
it.each([12, 20, 24])("downloads the References transformer for %s GiB of VRAM", async (vram) => {
const original = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => command === "weight_download_hardware"
? [{ name: "GPU", memoryBytes: vram * 1024 ** 3 }] : original(command, args));
await downloadTemplateWeights("minimax-h3-references");
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 4, error: null });
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({ url: vram === 20 ? "https://huggingface.co/Comfy-Org/MiniMax-H3/resolve/main/diffusion_models/minimax_h3_ref2va_pruned_w6a8.safetensors" : vram <= 20
? "https://huggingface.co/koongrizzly/MiniMax_H3_int4_W4A8_ConvRot_Pruned/resolve/main/diffusion_models/minimax_h3_ref2va_hybrid_b20-49_pruned_w4a8_mixed.safetensors"
: "https://huggingface.co/Comfy-Org/MiniMax-H3/resolve/main/diffusion_models/minimax_h3_ref2va_pruned_int8_convrot.safetensors" }));
expect(templateNeedsDownload(loadGeneratorTemplateSettings().templates.find(({ id }) => id === "minimax-h3-references")!)).toBe(false);
});
it.each([0, 12, 20, 24])("downloads only the FastH3 Omni transformer for %s GiB and retains its eight-step default", async (vram) => {
const original = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => command === "weight_download_hardware"
? vram ? [{ name: "GPU", memoryBytes: vram * 1024 ** 3 }] : [] : original(command, args));
await downloadTemplateWeights("minimax-h3-fast");
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 4, error: null });
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({
url: vram < 22 ? "https://huggingface.co/berryber09/FastH3-OmniRef-8step-pruned-w4a8/resolve/main/fasth3_omniref_8step_pruned_w4a8.safetensors" : "https://huggingface.co/FastVideo/FastH3-Omni-Comfy/resolve/main/diffusion_models/fasth3_omniref_8step_pruned_int8_convrot.safetensors",
}));
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({
url: "https://huggingface.co/Comfy-Org/MiniMax-H3/blob/main/vae/minimax_h3_video_vae_int8_convrot.safetensors",
}));
const template = loadGeneratorTemplateSettings().templates.find(({ id }) => id === "minimax-h3-fast")!;
expect(template.defaultSteps).toBe(8);
expect(templateNeedsDownload(template)).toBe(false);
});
it.each([0, 12, 20, 32])("downloads Singularity with four steps and the shared weights for %s GiB of VRAM", async (vram) => {
const original = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => command === "weight_download_hardware"
? (vram ? [{ name: "GPU", memoryBytes: vram * 1024 ** 3 }] : []) : original(command, args));
await downloadTemplateWeights("minimax-h3-singularity");
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 5, files: 5, error: null });
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({ url: TURBO_LORA.url }));
expect(loadLoras().find(({ id }) => id === TURBO_LORA.id)?.path).not.toBe("");
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({
url: vram < 21 ? "https://huggingface.co/WarmBloodAban/Minimax-h3_Singularity/resolve/main/Minimax-h3_Singularity_ref2va_v1.3_Pruned_w4a8.safetensors" : "https://huggingface.co/WarmBloodAban/Minimax-h3_Singularity/blob/main/Minimax-h3_Singularity_ref2va_Pruned_v1.3_int8.safetensors",
}));
for (const field of ["textEncoder", "videoVae", "audioVae"] as const) {
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({ url: minimaxOriginalTemplate().paths[field] }));
}
const settings = loadGeneratorTemplateSettings();
const template = settings.templates.find(({ id }) => id === "minimax-h3-singularity")!;
expect(template.paths.transformer).toBe(vram < 21 ? "C:/Slopus/weights/Minimax-h3_Singularity_ref2va_v1.3_Pruned_w4a8.safetensors" : "C:/Slopus/weights/Minimax-h3_Singularity_ref2va_Pruned_v1.3_int8.safetensors");
expect(templateNeedsDownload(template)).toBe(false);
saveGeneratorTemplateSettings({ ...settings, defaultTemplateId: template.id });
expect(defaultGeneratorTemplate().defaultSteps).toBe(4);
});
it.each([[12, "w4a8"], [16, "w6a8_r2"], [21, "int8_convrot_r2"]] as const)("downloads Singularity v2 %s GB weights with its four-step adapter", async (vram, format) => {
const original = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => command === "weight_download_hardware"
? [{ name: "GPU", memoryBytes: vram * 1024 ** 3 }] : original(command, args));
await downloadTemplateWeights("minimax-h3-singularity-v2");
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 5, files: 5, error: null });
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({
url: `https://huggingface.co/WAS/Minimax-h3_Singularity_v2_pruned/resolve/main/Minimax-h3_Singularity_ref2va_v2_pruned_${format}.safetensors`,
}));
expect(invoke).toHaveBeenCalledWith("download_weight", expect.objectContaining({ url: TURBO_LORA.url }));
const template = loadGeneratorTemplateSettings().templates.find(({ id }) => id === "minimax-h3-singularity-v2")!;
expect(template.defaultSteps).toBe(4);
expect(templateNeedsDownload(template)).toBe(false);
});
it("reuses downloaded template LoRAs and restores readiness when a shared adapter goes missing", async () => {
await downloadLora(TURBO_LORA.id);
vi.mocked(invoke).mockClear();
await downloadTemplateWeights("minimax-h3-singularity");
expect(getWeightDownloadState()).toMatchObject({ completed: 4, error: null });
expect(invoke).not.toHaveBeenCalledWith("download_weight", expect.objectContaining({ url: TURBO_LORA.url }));
const settings = loadGeneratorTemplateSettings();
const template = settings.templates.find(({ id }) => id === "minimax-h3-singularity")!;
saveGeneratorTemplateSettings({ ...settings, defaultTemplateId: template.id });
files.delete(loadLoras().find(({ id }) => id === TURBO_LORA.id)!.path);
await refreshDownloadedWeights();
expect(templateNeedsDownload(template)).toBe(true);
expect(loadGeneratorTemplateSettings().defaultTemplateId).toBe("default");
vi.mocked(invoke).mockClear();
await downloadTemplateWeights(template.id);
expect(getWeightDownloadState()).toMatchObject({ completed: 1, files: 1, error: null });
expect(templateNeedsDownload(template)).toBe(false);
expect(vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight")).toHaveLength(1);
});
it.each([{ enabled: false, strength: 1 }, { enabled: true, strength: 0 }])("skips inactive template LoRAs: %j", async (selection) => {
const template = createGeneratorTemplate();
template.loras = [{ loraId: TURBO_LORA.id, ...selection }];
Object.assign(template.paths, { textEncoder: "text", videoVae: "video", audioVae: "audio" });
["text", "video", "audio"].forEach((path) => files.add(path));
saveGeneratorTemplateSettings({ templates: [template], defaultTemplateId: template.id, catalogVersion: 6 });
expect(templateNeedsDownload(template)).toBe(false);
await downloadTemplateWeights(template.id);
expect(invoke).not.toHaveBeenCalledWith("download_weight", expect.anything());
expect(getWeightDownloadState()).toMatchObject({ files: 0, error: null });
});
it("keeps generator progress and cancellation during its LoRA transfer, then retries only the missing adapter", async () => {
const normal = vi.mocked(invoke).getMockImplementation()!;
let started!: () => void;
const downloadingLora = new Promise<void>((resolve) => { started = resolve; });
let rejectDownload!: (reason: Error) => void;
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "download_weight" && (args as { url: string }).url === TURBO_LORA.url) {
return new Promise((_resolve, reject) => { rejectDownload = reject; started(); });
}
if (command === "cancel_weight_download") { rejectDownload(new Error("Download cancelled.")); return; }
return normal(command, args);
});
const pending = downloadTemplateWeights("minimax-h3-singularity");
await downloadingLora;
const state = getWeightDownloadState()!;
expect(state).toMatchObject({ templateId: "minimax-h3-singularity", currentLoraId: TURBO_LORA.id, completed: 4, files: 5, active: true });
expect(weightDownloadProgress({ ...state, downloaded: 50, total: 100 })).toBe(90);
expect(templateNeedsDownload(loadGeneratorTemplateSettings().templates.find(({ id }) => id === state.templateId)!)).toBe(true);
await cancelWeightDownload();
await pending;
expect(getWeightDownloadState()).toMatchObject({ active: false, error: "Download cancelled." });
vi.mocked(invoke).mockImplementation(normal).mockClear();
await retryWeightDownload();
expect(getWeightDownloadState()).toMatchObject({ templateId: state.templateId, completed: 1, files: 1, error: null });
expect(vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight")).toHaveLength(1);
});
it("replaces a URL being typed instead of accumulating partial download sources", () => {
let template = createGeneratorTemplate();
for (const value of ["https://", "https://e", "https://example.com/model"]) template = updateWeightPath(template, "transformer", value);
expect(template.sources?.transformer).toEqual([source("https://example.com/model")]);
});
it("downloads all sample weights, saves local paths, and restores a deleted weight's URL", async () => {
await downloadTemplateWeights("minimax-h3-original");
const template = saved();
expect(templateNeedsDownload(template)).toBe(false);
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 4, files: 4, error: null });
const url = template.sources!.transformer![0].url;
expect(template.paths.transformer).toMatch(/^C:\/Slopus\/weights\//);
saveGeneratorTemplateSettings({ ...loadGeneratorTemplateSettings(), defaultTemplateId: template.id });
files.delete(template.paths.transformer);
await refreshDownloadedWeights();
expect(saved().paths.transformer).toBe(url);
expect(saved().sources!.transformer![0].downloadedPath).toBeUndefined();
expect(loadGeneratorTemplateSettings().defaultTemplateId).toBe("default");
vi.mocked(invoke).mockClear();
await downloadTemplateWeights(template.id);
expect(vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight")).toHaveLength(1);
expect(templateNeedsDownload(saved())).toBe(false);
});
it("removes downloaded files while retaining the template and original URLs", async () => {
await downloadTemplateWeights("minimax-h3-original");
await removeTemplateWeights("minimax-h3-original");
expect(files.size).toBe(3); // Shared VAEs and encoder remain installed.
expect(saved().paths.transformer).toBe(minimaxOriginalTemplate().paths.transformer);
expect(saved().paths.textEncoder).not.toMatch(/^https?:/);
expect(saved().sources!.transformer!.every((source) => !source.downloadedPath)).toBe(true);
expect(templateNeedsDownload(saved())).toBe(true);
await downloadTemplateWeights("minimax-h3-original");
expect(templateNeedsDownload(saved())).toBe(false);
});
it("never sends a manually selected local file to the removal command", async () => {
const template = minimaxOriginalTemplate();
template.paths.transformer = "D:/MyModels/personal.safetensors";
saveGeneratorTemplateSettings({ templates: [template], defaultTemplateId: "", catalogVersion: 1 });
await removeTemplateWeights(template.id);
expect(invoke).toHaveBeenCalledWith("remove_downloaded_weights", { paths: [] });
expect(saved().paths.transformer).toBe(minimaxOriginalTemplate().paths.transformer);
});
it("retains successful files when a later download fails and retries only unfinished weights", async () => {
const original = vi.mocked(invoke).getMockImplementation()!;
let attempts = 0;
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "download_weight" && ++attempts === 2) throw new Error("Network lost");
return original(command, args);
});
await downloadTemplateWeights("minimax-h3-original");
expect(isDownloadUrl(saved().paths.transformer)).toBe(false);
expect(isDownloadUrl(saved().paths.textEncoder)).toBe(true);
expect(getWeightDownloadState()).toMatchObject({ active: false, completed: 1, error: "Network lost" });
vi.mocked(invoke).mockImplementation(original);
vi.mocked(invoke).mockClear();
await downloadTemplateWeights("minimax-h3-original");
expect(vi.mocked(invoke).mock.calls.filter(([command]) => command === "download_weight")).toHaveLength(3);
});
it("preserves edits made while a download is running", async () => {
const original = vi.mocked(invoke).getMockImplementation()!;
vi.mocked(invoke).mockImplementation(async (command, args) => {
if (command === "download_weight") {
const settings = loadGeneratorTemplateSettings();
const template = settings.templates.find((template) => template.id === "minimax-h3-original")!;
template.paths.transformer = "D:/MyModels/replacement.safetensors";
saveGeneratorTemplateSettings(settings);
}
return original(command, args);
});
await downloadTemplateWeights("minimax-h3-original");
expect(saved().paths.transformer).toBe("D:/MyModels/replacement.safetensors");
});
it("restores all templates sharing a downloaded weight when one removes it", async () => {
await downloadTemplateWeights("minimax-h3-original");
const settings = loadGeneratorTemplateSettings();
settings.templates.push({ ...saved(), id: "copy", name: "Shared copy" });
saveGeneratorTemplateSettings(settings);
await removeTemplateWeights("minimax-h3-original");
const copy = loadGeneratorTemplateSettings().templates.find((template) => template.id === "copy")!;
expect(templateNeedsDownload(copy)).toBe(true);
expect(copy.paths.transformer).toBe(minimaxOriginalTemplate().paths.transformer);
expect(copy.paths.videoVae).not.toMatch(/^https?:/);
});