feat: expose plugin data storage API

This commit is contained in:
2026-06-29 04:17:33 +08:00
parent 663e299d28
commit 4387c6ab19
9 changed files with 58 additions and 4 deletions
+21
View File
@@ -12,6 +12,8 @@ describe('VerstakPluginAPI contract', () => {
expect(api.pluginId).toBe('verstak.platform-test');
expect(typeof api.settings.read).toBe('function');
expect(typeof api.settings.write).toBe('function');
expect(typeof api.storage.data.read).toBe('function');
expect(typeof api.storage.data.write).toBe('function');
expect(typeof api.capabilities.list).toBe('function');
expect(typeof api.commands.register).toBe('function');
expect(typeof api.commands.execute).toBe('function');
@@ -190,6 +192,25 @@ describe('VerstakPluginAPI contract', () => {
await expect(api.settings.read()).resolves.toEqual({ savedText: 'hello' });
});
test('plugin data persists separately from settings in the mock API namespace', async () => {
const api = createMockPluginAPI('storage.plugin');
await api.settings.write('search-index', { source: 'settings' });
await api.storage.data.write('search-index', {
version: 1,
workspaceRootPath: 'Project',
entries: [{ path: 'Project/Docs/case.md' }],
});
await expect(api.storage.data.read('search-index')).resolves.toEqual({
version: 1,
workspaceRootPath: 'Project',
entries: [{ path: 'Project/Docs/case.md' }],
});
await expect(api.settings.read('search-index')).resolves.toEqual({ source: 'settings' });
await expect(api.storage.data.read('missing')).resolves.toEqual({});
});
test('commands register, execute, and unregister', async () => {
const api = createMockPluginAPI('cmd.plugin');
+8
View File
@@ -22,6 +22,7 @@ import type {
} from './types';
export type PluginCommandArgs = Record<string, unknown>;
export type PluginDataJSON = Record<string, unknown>;
export type PluginCommandHandler = (
args: PluginCommandArgs,
declaration: PluginCommandDeclaration
@@ -96,6 +97,13 @@ export interface VerstakPluginAPI {
writeAll(settings: PluginSettings): Promise<void>;
};
storage: {
data: {
read(name: string): Promise<PluginDataJSON>;
write(name: string, data: PluginDataJSON): Promise<void>;
};
};
capabilities: {
has(capability: string): Promise<boolean>;
get(capability: string): Promise<{ available: boolean; name?: string; pluginId?: string; status?: string }>;
+9
View File
@@ -52,6 +52,7 @@ export function createTestPluginState(overrides?: Partial<PluginState>): PluginS
*/
export function createMockPluginAPI(pluginId = 'test.plugin', options: MockPluginAPIOptions = {}): VerstakPluginAPI {
const settings: Record<string, unknown> = {};
const pluginData = new Map<string, Record<string, unknown>>();
const commands = new Map<string, PluginCommandHandler>();
const eventHandlers = new Map<string, Array<(event: any) => void>>();
const files = new Map<string, { type: 'file' | 'folder'; content?: string; modifiedAt: string }>();
@@ -129,6 +130,14 @@ export function createMockPluginAPI(pluginId = 'test.plugin', options: MockPlugi
Object.assign(settings, nextSettings);
}),
},
storage: {
data: {
read: vi.fn(async (name: string) => ({ ...(pluginData.get(name) || {}) })),
write: vi.fn(async (name: string, data: Record<string, unknown>) => {
pluginData.set(name, { ...(data || {}) });
}),
},
},
capabilities: {
has: vi.fn(async () => false),
get: vi.fn(async (name: string) => ({ available: false, name })),