mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-04-05 23:46:16 +00:00
feat(api): add ResolveSpec and WebSocket client implementations
- Introduced ResolveSpecClient for REST API interactions. - Added WebSocketClient for real-time communication. - Created types and utility functions for both clients. - Removed deprecated types and example files. - Configured TypeScript and Vite for building the library.
This commit is contained in:
143
resolvespec-js/src/__tests__/common.test.ts
Normal file
143
resolvespec-js/src/__tests__/common.test.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import type {
|
||||
Options,
|
||||
FilterOption,
|
||||
SortOption,
|
||||
PreloadOption,
|
||||
RequestBody,
|
||||
APIResponse,
|
||||
Metadata,
|
||||
APIError,
|
||||
Parameter,
|
||||
ComputedColumn,
|
||||
CustomOperator,
|
||||
} from '../common/types';
|
||||
|
||||
describe('Common Types', () => {
|
||||
it('should construct a valid FilterOption with logic_operator', () => {
|
||||
const filter: FilterOption = {
|
||||
column: 'name',
|
||||
operator: 'eq',
|
||||
value: 'test',
|
||||
logic_operator: 'OR',
|
||||
};
|
||||
expect(filter.logic_operator).toBe('OR');
|
||||
expect(filter.operator).toBe('eq');
|
||||
});
|
||||
|
||||
it('should construct Options with all new fields', () => {
|
||||
const opts: Options = {
|
||||
columns: ['id', 'name'],
|
||||
omit_columns: ['secret'],
|
||||
filters: [{ column: 'age', operator: 'gte', value: 18 }],
|
||||
sort: [{ column: 'name', direction: 'asc' }],
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
cursor_forward: 'abc123',
|
||||
cursor_backward: 'xyz789',
|
||||
fetch_row_number: '42',
|
||||
parameters: [{ name: 'param1', value: 'val1', sequence: 1 }],
|
||||
computedColumns: [{ name: 'full_name', expression: "first || ' ' || last" }],
|
||||
customOperators: [{ name: 'custom', sql: "status = 'active'" }],
|
||||
preload: [{
|
||||
relation: 'Items',
|
||||
columns: ['id', 'title'],
|
||||
omit_columns: ['internal'],
|
||||
sort: [{ column: 'id', direction: 'ASC' }],
|
||||
recursive: true,
|
||||
primary_key: 'id',
|
||||
related_key: 'parent_id',
|
||||
sql_joins: ['LEFT JOIN other ON other.id = items.other_id'],
|
||||
join_aliases: ['other'],
|
||||
}],
|
||||
};
|
||||
expect(opts.omit_columns).toEqual(['secret']);
|
||||
expect(opts.cursor_forward).toBe('abc123');
|
||||
expect(opts.fetch_row_number).toBe('42');
|
||||
expect(opts.parameters![0].sequence).toBe(1);
|
||||
expect(opts.preload![0].recursive).toBe(true);
|
||||
});
|
||||
|
||||
it('should construct a RequestBody with numeric id', () => {
|
||||
const body: RequestBody = {
|
||||
operation: 'read',
|
||||
id: 42,
|
||||
options: { limit: 10 },
|
||||
};
|
||||
expect(body.id).toBe(42);
|
||||
});
|
||||
|
||||
it('should construct a RequestBody with string array id', () => {
|
||||
const body: RequestBody = {
|
||||
operation: 'delete',
|
||||
id: ['1', '2', '3'],
|
||||
};
|
||||
expect(Array.isArray(body.id)).toBe(true);
|
||||
});
|
||||
|
||||
it('should construct Metadata with count and row_number', () => {
|
||||
const meta: Metadata = {
|
||||
total: 100,
|
||||
count: 10,
|
||||
filtered: 50,
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
row_number: 5,
|
||||
};
|
||||
expect(meta.count).toBe(10);
|
||||
expect(meta.row_number).toBe(5);
|
||||
});
|
||||
|
||||
it('should construct APIError with detail field', () => {
|
||||
const err: APIError = {
|
||||
code: 'not_found',
|
||||
message: 'Record not found',
|
||||
detail: 'The record with id 42 does not exist',
|
||||
};
|
||||
expect(err.detail).toBeDefined();
|
||||
});
|
||||
|
||||
it('should construct APIResponse with metadata', () => {
|
||||
const resp: APIResponse<string[]> = {
|
||||
success: true,
|
||||
data: ['a', 'b'],
|
||||
metadata: { total: 2, count: 2, filtered: 2, limit: 10, offset: 0 },
|
||||
};
|
||||
expect(resp.metadata?.count).toBe(2);
|
||||
});
|
||||
|
||||
it('should support all operator types', () => {
|
||||
const operators: FilterOption['operator'][] = [
|
||||
'eq', 'neq', 'gt', 'gte', 'lt', 'lte',
|
||||
'like', 'ilike', 'in',
|
||||
'contains', 'startswith', 'endswith',
|
||||
'between', 'between_inclusive',
|
||||
'is_null', 'is_not_null',
|
||||
];
|
||||
for (const op of operators) {
|
||||
const f: FilterOption = { column: 'x', operator: op, value: 'v' };
|
||||
expect(f.operator).toBe(op);
|
||||
}
|
||||
});
|
||||
|
||||
it('should support PreloadOption with computed_ql and where', () => {
|
||||
const preload: PreloadOption = {
|
||||
relation: 'Details',
|
||||
where: "status = 'active'",
|
||||
computed_ql: { cql1: 'SUM(amount)' },
|
||||
table_name: 'detail_table',
|
||||
updatable: true,
|
||||
foreign_key: 'detail_id',
|
||||
recursive_child_key: 'parent_detail_id',
|
||||
};
|
||||
expect(preload.computed_ql?.cql1).toBe('SUM(amount)');
|
||||
expect(preload.updatable).toBe(true);
|
||||
});
|
||||
|
||||
it('should support Parameter interface', () => {
|
||||
const p: Parameter = { name: 'key', value: 'val' };
|
||||
expect(p.name).toBe('key');
|
||||
const p2: Parameter = { name: 'key2', value: 'val2', sequence: 5 };
|
||||
expect(p2.sequence).toBe(5);
|
||||
});
|
||||
});
|
||||
239
resolvespec-js/src/__tests__/headerspec.test.ts
Normal file
239
resolvespec-js/src/__tests__/headerspec.test.ts
Normal file
@@ -0,0 +1,239 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { buildHeaders, encodeHeaderValue, decodeHeaderValue, HeaderSpecClient, getHeaderSpecClient } from '../headerspec/client';
|
||||
import type { Options, ClientConfig, APIResponse } from '../common/types';
|
||||
|
||||
describe('buildHeaders', () => {
|
||||
it('should set X-Select-Fields for columns', () => {
|
||||
const h = buildHeaders({ columns: ['id', 'name', 'email'] });
|
||||
expect(h['X-Select-Fields']).toBe('id,name,email');
|
||||
});
|
||||
|
||||
it('should set X-Not-Select-Fields for omit_columns', () => {
|
||||
const h = buildHeaders({ omit_columns: ['secret', 'internal'] });
|
||||
expect(h['X-Not-Select-Fields']).toBe('secret,internal');
|
||||
});
|
||||
|
||||
it('should set X-FieldFilter for eq AND filters', () => {
|
||||
const h = buildHeaders({
|
||||
filters: [{ column: 'status', operator: 'eq', value: 'active' }],
|
||||
});
|
||||
expect(h['X-FieldFilter-status']).toBe('active');
|
||||
});
|
||||
|
||||
it('should set X-SearchOp for non-eq AND filters', () => {
|
||||
const h = buildHeaders({
|
||||
filters: [{ column: 'age', operator: 'gte', value: 18 }],
|
||||
});
|
||||
expect(h['X-SearchOp-greaterthanorequal-age']).toBe('18');
|
||||
});
|
||||
|
||||
it('should set X-SearchOr for OR filters', () => {
|
||||
const h = buildHeaders({
|
||||
filters: [{ column: 'name', operator: 'contains', value: 'test', logic_operator: 'OR' }],
|
||||
});
|
||||
expect(h['X-SearchOr-contains-name']).toBe('test');
|
||||
});
|
||||
|
||||
it('should set X-Sort with direction prefixes', () => {
|
||||
const h = buildHeaders({
|
||||
sort: [
|
||||
{ column: 'name', direction: 'asc' },
|
||||
{ column: 'created_at', direction: 'DESC' },
|
||||
],
|
||||
});
|
||||
expect(h['X-Sort']).toBe('+name,-created_at');
|
||||
});
|
||||
|
||||
it('should set X-Limit and X-Offset', () => {
|
||||
const h = buildHeaders({ limit: 25, offset: 50 });
|
||||
expect(h['X-Limit']).toBe('25');
|
||||
expect(h['X-Offset']).toBe('50');
|
||||
});
|
||||
|
||||
it('should set cursor pagination headers', () => {
|
||||
const h = buildHeaders({ cursor_forward: 'abc', cursor_backward: 'xyz' });
|
||||
expect(h['X-Cursor-Forward']).toBe('abc');
|
||||
expect(h['X-Cursor-Backward']).toBe('xyz');
|
||||
});
|
||||
|
||||
it('should set X-Preload with pipe-separated relations', () => {
|
||||
const h = buildHeaders({
|
||||
preload: [
|
||||
{ relation: 'Items', columns: ['id', 'name'] },
|
||||
{ relation: 'Category' },
|
||||
],
|
||||
});
|
||||
expect(h['X-Preload']).toBe('Items:id,name|Category');
|
||||
});
|
||||
|
||||
it('should set X-Fetch-RowNumber', () => {
|
||||
const h = buildHeaders({ fetch_row_number: '42' });
|
||||
expect(h['X-Fetch-RowNumber']).toBe('42');
|
||||
});
|
||||
|
||||
it('should set X-CQL-SEL for computed columns', () => {
|
||||
const h = buildHeaders({
|
||||
computedColumns: [
|
||||
{ name: 'total', expression: 'price * qty' },
|
||||
],
|
||||
});
|
||||
expect(h['X-CQL-SEL-total']).toBe('price * qty');
|
||||
});
|
||||
|
||||
it('should set X-Custom-SQL-W for custom operators', () => {
|
||||
const h = buildHeaders({
|
||||
customOperators: [
|
||||
{ name: 'active', sql: "status = 'active'" },
|
||||
{ name: 'verified', sql: "verified = true" },
|
||||
],
|
||||
});
|
||||
expect(h['X-Custom-SQL-W']).toBe("status = 'active' AND verified = true");
|
||||
});
|
||||
|
||||
it('should return empty object for empty options', () => {
|
||||
const h = buildHeaders({});
|
||||
expect(Object.keys(h)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should handle between filter with array value', () => {
|
||||
const h = buildHeaders({
|
||||
filters: [{ column: 'price', operator: 'between', value: [10, 100] }],
|
||||
});
|
||||
expect(h['X-SearchOp-between-price']).toBe('10,100');
|
||||
});
|
||||
|
||||
it('should handle is_null filter with null value', () => {
|
||||
const h = buildHeaders({
|
||||
filters: [{ column: 'deleted_at', operator: 'is_null', value: null }],
|
||||
});
|
||||
expect(h['X-SearchOp-empty-deleted_at']).toBe('');
|
||||
});
|
||||
|
||||
it('should handle in filter with array value', () => {
|
||||
const h = buildHeaders({
|
||||
filters: [{ column: 'id', operator: 'in', value: [1, 2, 3] }],
|
||||
});
|
||||
expect(h['X-SearchOp-in-id']).toBe('1,2,3');
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeHeaderValue / decodeHeaderValue', () => {
|
||||
it('should round-trip encode/decode', () => {
|
||||
const original = 'some complex value with spaces & symbols!';
|
||||
const encoded = encodeHeaderValue(original);
|
||||
expect(encoded.startsWith('ZIP_')).toBe(true);
|
||||
const decoded = decodeHeaderValue(encoded);
|
||||
expect(decoded).toBe(original);
|
||||
});
|
||||
|
||||
it('should decode __ prefixed values', () => {
|
||||
const encoded = '__' + btoa('hello');
|
||||
expect(decodeHeaderValue(encoded)).toBe('hello');
|
||||
});
|
||||
|
||||
it('should return plain values as-is', () => {
|
||||
expect(decodeHeaderValue('plain')).toBe('plain');
|
||||
});
|
||||
});
|
||||
|
||||
describe('HeaderSpecClient', () => {
|
||||
const config: ClientConfig = { baseUrl: 'http://localhost:3000', token: 'tok' };
|
||||
|
||||
function mockFetch<T>(data: APIResponse<T>, ok = true) {
|
||||
return vi.fn().mockResolvedValue({
|
||||
ok,
|
||||
json: () => Promise.resolve(data),
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('read() sends GET with headers from options', async () => {
|
||||
globalThis.fetch = mockFetch({ success: true, data: [{ id: 1 }] });
|
||||
const client = new HeaderSpecClient(config);
|
||||
|
||||
await client.read('public', 'users', undefined, {
|
||||
columns: ['id', 'name'],
|
||||
limit: 10,
|
||||
});
|
||||
|
||||
const [url, opts] = (globalThis.fetch as any).mock.calls[0];
|
||||
expect(url).toBe('http://localhost:3000/public/users');
|
||||
expect(opts.method).toBe('GET');
|
||||
expect(opts.headers['X-Select-Fields']).toBe('id,name');
|
||||
expect(opts.headers['X-Limit']).toBe('10');
|
||||
expect(opts.headers['Authorization']).toBe('Bearer tok');
|
||||
});
|
||||
|
||||
it('read() with id appends to URL', async () => {
|
||||
globalThis.fetch = mockFetch({ success: true, data: {} });
|
||||
const client = new HeaderSpecClient(config);
|
||||
|
||||
await client.read('public', 'users', '42');
|
||||
|
||||
const [url] = (globalThis.fetch as any).mock.calls[0];
|
||||
expect(url).toBe('http://localhost:3000/public/users/42');
|
||||
});
|
||||
|
||||
it('create() sends POST with body and headers', async () => {
|
||||
globalThis.fetch = mockFetch({ success: true, data: { id: 1 } });
|
||||
const client = new HeaderSpecClient(config);
|
||||
|
||||
await client.create('public', 'users', { name: 'Test' });
|
||||
|
||||
const [url, opts] = (globalThis.fetch as any).mock.calls[0];
|
||||
expect(opts.method).toBe('POST');
|
||||
expect(JSON.parse(opts.body)).toEqual({ name: 'Test' });
|
||||
});
|
||||
|
||||
it('update() sends PUT with id in URL', async () => {
|
||||
globalThis.fetch = mockFetch({ success: true, data: {} });
|
||||
const client = new HeaderSpecClient(config);
|
||||
|
||||
await client.update('public', 'users', '1', { name: 'Updated' }, {
|
||||
filters: [{ column: 'active', operator: 'eq', value: true }],
|
||||
});
|
||||
|
||||
const [url, opts] = (globalThis.fetch as any).mock.calls[0];
|
||||
expect(url).toBe('http://localhost:3000/public/users/1');
|
||||
expect(opts.method).toBe('PUT');
|
||||
expect(opts.headers['X-FieldFilter-active']).toBe('true');
|
||||
});
|
||||
|
||||
it('delete() sends DELETE', async () => {
|
||||
globalThis.fetch = mockFetch({ success: true, data: undefined as any });
|
||||
const client = new HeaderSpecClient(config);
|
||||
|
||||
await client.delete('public', 'users', '1');
|
||||
|
||||
const [url, opts] = (globalThis.fetch as any).mock.calls[0];
|
||||
expect(url).toBe('http://localhost:3000/public/users/1');
|
||||
expect(opts.method).toBe('DELETE');
|
||||
});
|
||||
|
||||
it('throws on non-ok response', async () => {
|
||||
globalThis.fetch = mockFetch(
|
||||
{ success: false, data: null as any, error: { code: 'err', message: 'fail' } },
|
||||
false
|
||||
);
|
||||
const client = new HeaderSpecClient(config);
|
||||
|
||||
await expect(client.read('public', 'users')).rejects.toThrow('fail');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getHeaderSpecClient singleton', () => {
|
||||
it('returns same instance for same baseUrl', () => {
|
||||
const a = getHeaderSpecClient({ baseUrl: 'http://hs-singleton:3000' });
|
||||
const b = getHeaderSpecClient({ baseUrl: 'http://hs-singleton:3000' });
|
||||
expect(a).toBe(b);
|
||||
});
|
||||
|
||||
it('returns different instances for different baseUrls', () => {
|
||||
const a = getHeaderSpecClient({ baseUrl: 'http://hs-singleton-a:3000' });
|
||||
const b = getHeaderSpecClient({ baseUrl: 'http://hs-singleton-b:3000' });
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
});
|
||||
178
resolvespec-js/src/__tests__/resolvespec.test.ts
Normal file
178
resolvespec-js/src/__tests__/resolvespec.test.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { ResolveSpecClient, getResolveSpecClient } from '../resolvespec/client';
|
||||
import type { ClientConfig, APIResponse } from '../common/types';
|
||||
|
||||
const config: ClientConfig = { baseUrl: 'http://localhost:3000', token: 'test-token' };
|
||||
|
||||
function mockFetchResponse<T>(data: APIResponse<T>, ok = true, status = 200) {
|
||||
return vi.fn().mockResolvedValue({
|
||||
ok,
|
||||
status,
|
||||
json: () => Promise.resolve(data),
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('ResolveSpecClient', () => {
|
||||
it('read() sends POST with operation read', async () => {
|
||||
const response: APIResponse = { success: true, data: [{ id: 1 }] };
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
const result = await client.read('public', 'users', 1);
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const [url, opts] = (globalThis.fetch as any).mock.calls[0];
|
||||
expect(url).toBe('http://localhost:3000/public/users/1');
|
||||
expect(opts.method).toBe('POST');
|
||||
expect(opts.headers['Authorization']).toBe('Bearer test-token');
|
||||
|
||||
const body = JSON.parse(opts.body);
|
||||
expect(body.operation).toBe('read');
|
||||
});
|
||||
|
||||
it('read() with string array id puts id in body', async () => {
|
||||
const response: APIResponse = { success: true, data: [] };
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
await client.read('public', 'users', ['1', '2']);
|
||||
const body = JSON.parse((globalThis.fetch as any).mock.calls[0][1].body);
|
||||
expect(body.id).toEqual(['1', '2']);
|
||||
});
|
||||
|
||||
it('read() passes options through', async () => {
|
||||
const response: APIResponse = { success: true, data: [] };
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
await client.read('public', 'users', undefined, {
|
||||
columns: ['id', 'name'],
|
||||
omit_columns: ['secret'],
|
||||
filters: [{ column: 'active', operator: 'eq', value: true }],
|
||||
sort: [{ column: 'name', direction: 'asc' }],
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
cursor_forward: 'cursor1',
|
||||
fetch_row_number: '5',
|
||||
});
|
||||
|
||||
const body = JSON.parse((globalThis.fetch as any).mock.calls[0][1].body);
|
||||
expect(body.options.columns).toEqual(['id', 'name']);
|
||||
expect(body.options.omit_columns).toEqual(['secret']);
|
||||
expect(body.options.cursor_forward).toBe('cursor1');
|
||||
expect(body.options.fetch_row_number).toBe('5');
|
||||
});
|
||||
|
||||
it('create() sends POST with operation create and data', async () => {
|
||||
const response: APIResponse = { success: true, data: { id: 1, name: 'Test' } };
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
const result = await client.create('public', 'users', { name: 'Test' });
|
||||
expect(result.data.name).toBe('Test');
|
||||
|
||||
const body = JSON.parse((globalThis.fetch as any).mock.calls[0][1].body);
|
||||
expect(body.operation).toBe('create');
|
||||
expect(body.data.name).toBe('Test');
|
||||
});
|
||||
|
||||
it('update() with single id puts id in URL', async () => {
|
||||
const response: APIResponse = { success: true, data: { id: 1 } };
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
await client.update('public', 'users', { name: 'Updated' }, 1);
|
||||
const [url] = (globalThis.fetch as any).mock.calls[0];
|
||||
expect(url).toBe('http://localhost:3000/public/users/1');
|
||||
});
|
||||
|
||||
it('update() with string array id puts id in body', async () => {
|
||||
const response: APIResponse = { success: true, data: {} };
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
await client.update('public', 'users', { active: false }, ['1', '2']);
|
||||
const body = JSON.parse((globalThis.fetch as any).mock.calls[0][1].body);
|
||||
expect(body.id).toEqual(['1', '2']);
|
||||
});
|
||||
|
||||
it('delete() sends POST with operation delete', async () => {
|
||||
const response: APIResponse<void> = { success: true, data: undefined as any };
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
await client.delete('public', 'users', 1);
|
||||
const [url, opts] = (globalThis.fetch as any).mock.calls[0];
|
||||
expect(url).toBe('http://localhost:3000/public/users/1');
|
||||
|
||||
const body = JSON.parse(opts.body);
|
||||
expect(body.operation).toBe('delete');
|
||||
});
|
||||
|
||||
it('getMetadata() sends GET request', async () => {
|
||||
const response: APIResponse = {
|
||||
success: true,
|
||||
data: { schema: 'public', table: 'users', columns: [], relations: [] },
|
||||
};
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
const result = await client.getMetadata('public', 'users');
|
||||
expect(result.data.table).toBe('users');
|
||||
|
||||
const opts = (globalThis.fetch as any).mock.calls[0][1];
|
||||
expect(opts.method).toBe('GET');
|
||||
});
|
||||
|
||||
it('throws on non-ok response', async () => {
|
||||
const errorResp = {
|
||||
success: false,
|
||||
data: null,
|
||||
error: { code: 'not_found', message: 'Not found' },
|
||||
};
|
||||
globalThis.fetch = mockFetchResponse(errorResp as any, false, 404);
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
await expect(client.read('public', 'users', 999)).rejects.toThrow('Not found');
|
||||
});
|
||||
|
||||
it('throws generic error when no error message', async () => {
|
||||
globalThis.fetch = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.resolve({ success: false, data: null }),
|
||||
});
|
||||
|
||||
const client = new ResolveSpecClient(config);
|
||||
await expect(client.read('public', 'users')).rejects.toThrow('An error occurred');
|
||||
});
|
||||
|
||||
it('config without token omits Authorization header', async () => {
|
||||
const noAuthConfig: ClientConfig = { baseUrl: 'http://localhost:3000' };
|
||||
const response: APIResponse = { success: true, data: [] };
|
||||
globalThis.fetch = mockFetchResponse(response);
|
||||
|
||||
const client = new ResolveSpecClient(noAuthConfig);
|
||||
await client.read('public', 'users');
|
||||
const opts = (globalThis.fetch as any).mock.calls[0][1];
|
||||
expect(opts.headers['Authorization']).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getResolveSpecClient singleton', () => {
|
||||
it('returns same instance for same baseUrl', () => {
|
||||
const a = getResolveSpecClient({ baseUrl: 'http://singleton-test:3000' });
|
||||
const b = getResolveSpecClient({ baseUrl: 'http://singleton-test:3000' });
|
||||
expect(a).toBe(b);
|
||||
});
|
||||
|
||||
it('returns different instances for different baseUrls', () => {
|
||||
const a = getResolveSpecClient({ baseUrl: 'http://singleton-a:3000' });
|
||||
const b = getResolveSpecClient({ baseUrl: 'http://singleton-b:3000' });
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
});
|
||||
336
resolvespec-js/src/__tests__/websocketspec.test.ts
Normal file
336
resolvespec-js/src/__tests__/websocketspec.test.ts
Normal file
@@ -0,0 +1,336 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { WebSocketClient, getWebSocketClient } from '../websocketspec/client';
|
||||
import type { WebSocketClientConfig } from '../websocketspec/types';
|
||||
|
||||
// Mock uuid
|
||||
vi.mock('uuid', () => ({
|
||||
v4: vi.fn(() => 'mock-uuid-1234'),
|
||||
}));
|
||||
|
||||
// Mock WebSocket
|
||||
class MockWebSocket {
|
||||
static OPEN = 1;
|
||||
static CLOSED = 3;
|
||||
|
||||
url: string;
|
||||
readyState = MockWebSocket.OPEN;
|
||||
onopen: ((ev: any) => void) | null = null;
|
||||
onclose: ((ev: any) => void) | null = null;
|
||||
onmessage: ((ev: any) => void) | null = null;
|
||||
onerror: ((ev: any) => void) | null = null;
|
||||
|
||||
private sentMessages: string[] = [];
|
||||
|
||||
constructor(url: string) {
|
||||
this.url = url;
|
||||
// Simulate async open
|
||||
setTimeout(() => {
|
||||
this.onopen?.({});
|
||||
}, 0);
|
||||
}
|
||||
|
||||
send(data: string) {
|
||||
this.sentMessages.push(data);
|
||||
}
|
||||
|
||||
close() {
|
||||
this.readyState = MockWebSocket.CLOSED;
|
||||
this.onclose?.({ code: 1000, reason: 'Normal closure' } as any);
|
||||
}
|
||||
|
||||
getSentMessages(): any[] {
|
||||
return this.sentMessages.map((m) => JSON.parse(m));
|
||||
}
|
||||
|
||||
simulateMessage(data: any) {
|
||||
this.onmessage?.({ data: JSON.stringify(data) });
|
||||
}
|
||||
}
|
||||
|
||||
let mockWsInstance: MockWebSocket | null = null;
|
||||
|
||||
beforeEach(() => {
|
||||
mockWsInstance = null;
|
||||
(globalThis as any).WebSocket = class extends MockWebSocket {
|
||||
constructor(url: string) {
|
||||
super(url);
|
||||
mockWsInstance = this;
|
||||
}
|
||||
};
|
||||
(globalThis as any).WebSocket.OPEN = MockWebSocket.OPEN;
|
||||
(globalThis as any).WebSocket.CLOSED = MockWebSocket.CLOSED;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('WebSocketClient', () => {
|
||||
const wsConfig: WebSocketClientConfig = {
|
||||
url: 'ws://localhost:8080',
|
||||
reconnect: false,
|
||||
heartbeatInterval: 60000,
|
||||
};
|
||||
|
||||
it('should connect and set state to connected', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
expect(client.getState()).toBe('connected');
|
||||
expect(client.isConnected()).toBe(true);
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should disconnect and set state to disconnected', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
client.disconnect();
|
||||
expect(client.getState()).toBe('disconnected');
|
||||
expect(client.isConnected()).toBe(false);
|
||||
});
|
||||
|
||||
it('should send read request', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
|
||||
const readPromise = client.read('users', {
|
||||
schema: 'public',
|
||||
filters: [{ column: 'active', operator: 'eq', value: true }],
|
||||
limit: 10,
|
||||
});
|
||||
|
||||
// Simulate server response
|
||||
const sent = mockWsInstance!.getSentMessages();
|
||||
expect(sent.length).toBe(1);
|
||||
expect(sent[0].operation).toBe('read');
|
||||
expect(sent[0].entity).toBe('users');
|
||||
expect(sent[0].options.filters[0].column).toBe('active');
|
||||
|
||||
mockWsInstance!.simulateMessage({
|
||||
id: sent[0].id,
|
||||
type: 'response',
|
||||
success: true,
|
||||
data: [{ id: 1 }],
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
const result = await readPromise;
|
||||
expect(result).toEqual([{ id: 1 }]);
|
||||
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should send create request', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
|
||||
const createPromise = client.create('users', { name: 'Test' }, { schema: 'public' });
|
||||
|
||||
const sent = mockWsInstance!.getSentMessages();
|
||||
expect(sent[0].operation).toBe('create');
|
||||
expect(sent[0].data.name).toBe('Test');
|
||||
|
||||
mockWsInstance!.simulateMessage({
|
||||
id: sent[0].id,
|
||||
type: 'response',
|
||||
success: true,
|
||||
data: { id: 1, name: 'Test' },
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
const result = await createPromise;
|
||||
expect(result.name).toBe('Test');
|
||||
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should send update request with record_id', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
|
||||
const updatePromise = client.update('users', '1', { name: 'Updated' });
|
||||
|
||||
const sent = mockWsInstance!.getSentMessages();
|
||||
expect(sent[0].operation).toBe('update');
|
||||
expect(sent[0].record_id).toBe('1');
|
||||
|
||||
mockWsInstance!.simulateMessage({
|
||||
id: sent[0].id,
|
||||
type: 'response',
|
||||
success: true,
|
||||
data: { id: 1, name: 'Updated' },
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
await updatePromise;
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should send delete request', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
|
||||
const deletePromise = client.delete('users', '1');
|
||||
|
||||
const sent = mockWsInstance!.getSentMessages();
|
||||
expect(sent[0].operation).toBe('delete');
|
||||
expect(sent[0].record_id).toBe('1');
|
||||
|
||||
mockWsInstance!.simulateMessage({
|
||||
id: sent[0].id,
|
||||
type: 'response',
|
||||
success: true,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
await deletePromise;
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should reject on failed request', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
|
||||
const readPromise = client.read('users');
|
||||
|
||||
const sent = mockWsInstance!.getSentMessages();
|
||||
mockWsInstance!.simulateMessage({
|
||||
id: sent[0].id,
|
||||
type: 'response',
|
||||
success: false,
|
||||
error: { code: 'not_found', message: 'Not found' },
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
await expect(readPromise).rejects.toThrow('Not found');
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should handle subscriptions', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
|
||||
const callback = vi.fn();
|
||||
const subPromise = client.subscribe('users', callback, {
|
||||
schema: 'public',
|
||||
});
|
||||
|
||||
const sent = mockWsInstance!.getSentMessages();
|
||||
expect(sent[0].type).toBe('subscription');
|
||||
expect(sent[0].operation).toBe('subscribe');
|
||||
|
||||
mockWsInstance!.simulateMessage({
|
||||
id: sent[0].id,
|
||||
type: 'response',
|
||||
success: true,
|
||||
data: { subscription_id: 'sub-1' },
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
const subId = await subPromise;
|
||||
expect(subId).toBe('sub-1');
|
||||
expect(client.getSubscriptions()).toHaveLength(1);
|
||||
|
||||
// Simulate notification
|
||||
mockWsInstance!.simulateMessage({
|
||||
type: 'notification',
|
||||
operation: 'create',
|
||||
subscription_id: 'sub-1',
|
||||
entity: 'users',
|
||||
data: { id: 2, name: 'New' },
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
expect(callback).toHaveBeenCalledTimes(1);
|
||||
expect(callback.mock.calls[0][0].data.id).toBe(2);
|
||||
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should handle unsubscribe', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
|
||||
// Subscribe first
|
||||
const subPromise = client.subscribe('users', vi.fn());
|
||||
let sent = mockWsInstance!.getSentMessages();
|
||||
mockWsInstance!.simulateMessage({
|
||||
id: sent[0].id,
|
||||
type: 'response',
|
||||
success: true,
|
||||
data: { subscription_id: 'sub-1' },
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
await subPromise;
|
||||
|
||||
// Unsubscribe
|
||||
const unsubPromise = client.unsubscribe('sub-1');
|
||||
sent = mockWsInstance!.getSentMessages();
|
||||
mockWsInstance!.simulateMessage({
|
||||
id: sent[sent.length - 1].id,
|
||||
type: 'response',
|
||||
success: true,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
await unsubPromise;
|
||||
expect(client.getSubscriptions()).toHaveLength(0);
|
||||
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should emit events', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
const connectCb = vi.fn();
|
||||
const stateChangeCb = vi.fn();
|
||||
|
||||
client.on('connect', connectCb);
|
||||
client.on('stateChange', stateChangeCb);
|
||||
|
||||
await client.connect();
|
||||
|
||||
expect(connectCb).toHaveBeenCalledTimes(1);
|
||||
expect(stateChangeCb).toHaveBeenCalled();
|
||||
|
||||
client.off('connect');
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should reject when sending without connection', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await expect(client.read('users')).rejects.toThrow('WebSocket is not connected');
|
||||
});
|
||||
|
||||
it('should handle pong messages without error', async () => {
|
||||
const client = new WebSocketClient(wsConfig);
|
||||
await client.connect();
|
||||
|
||||
// Should not throw
|
||||
mockWsInstance!.simulateMessage({ type: 'pong' });
|
||||
|
||||
client.disconnect();
|
||||
});
|
||||
|
||||
it('should handle malformed messages gracefully', async () => {
|
||||
const client = new WebSocketClient({ ...wsConfig, debug: false });
|
||||
await client.connect();
|
||||
|
||||
// Simulate non-JSON message
|
||||
mockWsInstance!.onmessage?.({ data: 'not-json' } as any);
|
||||
|
||||
client.disconnect();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getWebSocketClient singleton', () => {
|
||||
it('returns same instance for same url', () => {
|
||||
const a = getWebSocketClient({ url: 'ws://ws-singleton:8080' });
|
||||
const b = getWebSocketClient({ url: 'ws://ws-singleton:8080' });
|
||||
expect(a).toBe(b);
|
||||
});
|
||||
|
||||
it('returns different instances for different urls', () => {
|
||||
const a = getWebSocketClient({ url: 'ws://ws-singleton-a:8080' });
|
||||
const b = getWebSocketClient({ url: 'ws://ws-singleton-b:8080' });
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user