Files
makelore/tests/unit/default-plugin-installation.test.ts
T

119 lines
5.9 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it, vi } from 'vitest';
import { createCodingPluginMarketplaceService } from '../../electron/api/coding-product-services';
import type { MarketplaceLibrarySnapshot } from '../../electron/coding-plugins/account-plugin-cache';
const pluginId = 'makelore.child-friendly-replies';
function fixture() {
let binding = { accountKey: 'a'.repeat(64), epoch: 1 };
let library: MarketplaceLibrarySnapshot = { items: [], total: 0, stale: false, fetchedAt: 1 };
let installed: object | null = null;
const entry = {
pluginId, title: '对孩子说清楚', summary: '简明表达', category: 'communication',
acquisition: 'free' as const, acquisitionMode: 'user_acquired' as const,
catalogStatus: 'active' as const, runtimeStatus: 'enabled' as const,
acquiredAt: '2026-09-28T00:00:00Z', removedAt: null, stableVersion: '1.0.0', betaVersion: null,
};
const marketplace = {
getCurrentAccountBinding: () => binding,
readLibrary: vi.fn(async () => library),
readDetail: vi.fn(async () => ({
pluginId, acquisition: 'free', runtimeStatus: 'enabled', stale: false,
stableRelease: { deliveryKind: 'artifact', signingKeyId: 'platform', descriptorSignature: 'signed',
minMakeloreVersion: '2.0.0', maxMakeloreVersion: null },
})),
acquire: vi.fn(async () => (library = { ...library, items: [entry], total: 1 })),
remove: vi.fn(async () => (library = {
...library, items: [{ ...entry, removedAt: '2026-09-28T01:00:00Z' }], total: 1,
})),
};
const packageStore = {
getInstalled: vi.fn(async () => installed),
resolveAndInstall: vi.fn(async () => {
installed = { pluginId, releaseId: 'child-1', version: '1.0.0', channel: 'stable' };
return { ...installed, status: 'installed' };
}),
uninstall: vi.fn(async () => { installed = null; return { pluginId, status: 'removed' }; }),
};
const onChanged = vi.fn();
const service = createCodingPluginMarketplaceService({
marketplace: marketplace as never, packageStore: packageStore as never,
clientVersion: '2.0.0', onChanged,
});
return { service, marketplace, packageStore, onChanged,
switchAccount() { binding = { accountKey: 'b'.repeat(64), epoch: 2 }; },
};
}
describe('default child-friendly replies installation', () => {
it('acquires and installs without a manual action, then leaves the installed package alone', async () => {
const f = fixture();
const result = await f.service.readLibrary();
expect(result.installations).toEqual([{ pluginId, status: 'installed', releaseId: 'child-1', version: '1.0.0', channel: 'stable' }]);
expect(f.marketplace.acquire).toHaveBeenCalledWith(pluginId);
expect(f.packageStore.resolveAndInstall).toHaveBeenCalledWith({ pluginId, makeloreVersion: '2.0.0', channel: 'stable' });
expect(f.onChanged).toHaveBeenCalledWith({ pluginId, kind: 'install' });
await f.service.readLibrary();
expect(f.packageStore.resolveAndInstall).toHaveBeenCalledTimes(1);
});
it('respects account removal and default-plugin device uninstall on later refreshes', async () => {
const f = fixture();
await f.service.readLibrary();
await f.service.uninstall(pluginId);
expect(f.marketplace.remove).toHaveBeenCalledWith(pluginId);
expect((await f.service.readLibrary()).installations).toEqual([]);
expect(f.packageStore.resolveAndInstall).toHaveBeenCalledTimes(1);
});
it('does not acquire an unsigned release or mistake it for installed', async () => {
const f = fixture();
f.marketplace.readDetail.mockImplementationOnce(async () => ({
pluginId, acquisition: 'free', runtimeStatus: 'enabled', stale: false,
stableRelease: { deliveryKind: 'artifact', signingKeyId: '', descriptorSignature: '',
minMakeloreVersion: '2.0.0', maxMakeloreVersion: null },
}));
expect((await f.service.readLibrary()).installations).toEqual([]);
expect(f.marketplace.acquire).not.toHaveBeenCalled();
expect(f.packageStore.resolveAndInstall).not.toHaveBeenCalled();
expect((await f.service.readLibrary()).installations).toHaveLength(1);
});
it('retries a failed download on refresh without duplicating acquisition', async () => {
const f = fixture();
f.packageStore.resolveAndInstall.mockRejectedValueOnce(new Error('download unavailable'));
expect((await f.service.readLibrary()).installations).toEqual([]);
expect((await f.service.readLibrary()).installations).toHaveLength(1);
expect(f.marketplace.acquire).toHaveBeenCalledTimes(1);
});
it('does not acquire for a different account when login changes during the detail request', async () => {
const f = fixture();
const original = f.marketplace.readDetail.getMockImplementation()!;
f.marketplace.readDetail.mockImplementationOnce(async () => {
f.switchAccount();
return original();
});
await expect(f.service.readLibrary()).rejects.toMatchObject({ code: 'marketplace_account_changed' });
expect(f.marketplace.acquire).not.toHaveBeenCalled();
expect(f.packageStore.resolveAndInstall).not.toHaveBeenCalled();
});
it('keeps an explicit removal made during automatic installation as the final choice', async () => {
const f = fixture();
let finish!: () => void;
const paused = new Promise<void>((resolve) => { finish = resolve; });
const install = f.packageStore.resolveAndInstall.getMockImplementation()!;
f.packageStore.resolveAndInstall.mockImplementationOnce(async () => { await paused; return install(); });
const refreshing = f.service.readLibrary();
await vi.waitFor(() => expect(f.packageStore.resolveAndInstall).toHaveBeenCalled());
const removing = f.service.remove(pluginId);
finish();
await refreshing;
await removing;
const result = await f.service.readLibrary();
expect(result.library.items[0].removedAt).toBeTruthy();
expect(f.packageStore.resolveAndInstall).toHaveBeenCalledTimes(1);
});
});