Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(app): fix tests

synoetfd4d01f8699e
diff --git a/js/app/package.json b/js/app/package.json
index e25be453b6..9f54ad3e81 100644
--- a/js/app/package.json
+++ b/js/app/package.json
@@ -22,7 +22,8 @@
"lint:ci": "bunx --bun biome ci --changed",
"format": "bunx --bun biome check --linter-enabled=false --write --staged",
"check-tailwind": "bun scripts/check-tailwind.ts",
- "test": "vitest",
+ "test": "vitest run",
+ "test:watch": "vitest",
"java:build": "bun scripts/createBackend.js",
"csharp:build": "bun scripts/publish-docx-server.js",
"clean-env": "bun scripts/clean-env.js",
diff --git a/js/app/packages/core/util/serviceToVertexAi.spec.ts b/js/app/packages/core/util/serviceToVertexAi.spec.ts
deleted file mode 100644
index 441f92ff60..0000000000
--- a/js/app/packages/core/util/serviceToVertexAi.spec.ts
+++ /dev/null
@@ -1,259 +0,0 @@
-import { describe, expect, test } from 'vitest';
-import { z } from 'zod';
-import { Svc } from '../service';
-import { convertSvcToVertexAI, SchemaType } from './serviceToVertexAi';
-
-describe('Basic functionality', () => {
- test('convert a simple service with one function', () => {
- const svc = new Svc('Test Service').fn('testFn', {
- description: 'Test function',
- args: { input: z.string().describe('Input string') },
- result: { output: z.number().describe('Output number') },
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result).toEqual({
- testFn: {
- name: 'testFn',
- description: 'Test function',
- parameters: {
- type: SchemaType.OBJECT,
- properties: {
- input: { type: SchemaType.STRING, description: 'Input string' },
- },
- required: ['input'],
- },
- returns: {
- type: SchemaType.OBJECT,
- properties: {
- output: { type: SchemaType.NUMBER, description: 'Output number' },
- },
- required: ['output'],
- },
- },
- });
- });
-
- test('a service with no functions', () => {
- const svc = new Svc('Empty Service');
- const result = convertSvcToVertexAI(svc);
- expect(result).toEqual({});
- });
-});
-
-describe('Access control', () => {
- test('exclude functions not accessible to AI', () => {
- const svc = new Svc('Service with access control')
- .fn('aiFunction', {
- description: 'AI can access this',
- args: { input: z.string() },
- })
- .fn('userOnlyFunction', {
- description: 'AI cannot access this',
- args: { input: z.string() },
- access: { only: ['user'] },
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result).toHaveProperty('aiFunction');
- expect(result).not.toHaveProperty('userOnlyFunction');
- });
-
- test('exclude entire service if AI is excluded', () => {
- const svc = new Svc('AI excluded service', {
- access: { exclude: ['ai'] },
- }).fn('someFunction', {
- description: 'Some function',
- args: { input: z.string() },
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result).toEqual({});
- });
-});
-
-describe('Nested services', () => {
- test('nested services', () => {
- const nestedSvc = new Svc('Nested Service').fn('nestedFn', {
- description: 'Nested function',
- args: { input: z.number() },
- });
-
- const mainSvc = new Svc('Main Service')
- .fn('mainFn', {
- description: 'Main function',
- args: { input: z.string() },
- })
- .use('nested', nestedSvc);
-
- const result = convertSvcToVertexAI(mainSvc);
-
- expect(result).toHaveProperty('mainFn');
- expect(result).toHaveProperty('nested');
- expect(result.nested).toHaveProperty('nestedFn');
- });
-});
-
-describe('Zod types', () => {
- test('Zod enums', () => {
- const MyEnum = z.enum(['Value1', 'Value2', 'Value3']);
-
- const svc = new Svc('Enum Service').fn('enumFn', {
- description: 'Enum function',
- args: { enumArg: MyEnum },
- result: { enumResult: MyEnum },
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result.enumFn.parameters.properties.enumArg).toEqual({
- type: SchemaType.STRING,
- enum: ['Value1', 'Value2', 'Value3'],
- });
- expect(result.enumFn.returns.properties.enumResult).toEqual({
- type: SchemaType.STRING,
- enum: ['Value1', 'Value2', 'Value3'],
- });
- });
-
- test('optional fields', () => {
- const svc = new Svc('Optional Fields Service').fn('optionalFn', {
- description: 'Function with optional field',
- args: {
- required: z.string(),
- optional: z.number().optional(),
- nullable: z.boolean().nullable(),
- requiredNullable: z.string().nullable(),
- },
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result.optionalFn.parameters).toEqual({
- type: SchemaType.OBJECT,
- properties: {
- required: { type: SchemaType.STRING },
- optional: { type: SchemaType.NUMBER, nullable: true },
- nullable: { type: SchemaType.BOOLEAN, nullable: true },
- requiredNullable: { type: SchemaType.STRING, nullable: true },
- },
- required: ['required'],
- });
- });
-
- test('arrays', () => {
- const svc = new Svc('Array Service').fn('arrayFn', {
- description: 'Function with array',
- args: { arrayArg: z.array(z.string()) },
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result.arrayFn.parameters.properties.arrayArg).toEqual({
- type: SchemaType.ARRAY,
- items: { type: SchemaType.STRING },
- });
- });
-
- test('nested objects', () => {
- const svc = new Svc('Nested Object Service').fn('nestedObjFn', {
- description: 'Function with nested object',
- args: {
- nestedObj: z.object({
- field1: z.string(),
- field2: z.number(),
- }),
- },
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result.nestedObjFn.parameters.properties.nestedObj).toEqual({
- type: SchemaType.OBJECT,
- properties: {
- field1: { type: SchemaType.STRING },
- field2: { type: SchemaType.NUMBER },
- },
- required: ['field1', 'field2'],
- });
- });
-});
-
-describe('Error handling', () => {
- test('throw error for unhandled Zod types', () => {
- const svc = new Svc('Unhandled Type Service').fn('unhandledTypeFn', {
- description: 'Function with unhandled type',
- args: { date: z.date() },
- });
-
- expect(() => convertSvcToVertexAI(svc)).toThrow(
- 'Unhandled Zod type: ZodDate'
- );
- });
-});
-
-describe('MaybeResult handling', () => {
- test('MaybeResult return type', () => {
- const svc = new Svc('MaybeResult Service')
- .err('ERROR_1', { description: 'This is error 1' })
- .err('ERROR_2', { description: 'This is error 2' })
- .fn('maybeResultFn', {
- description: 'Function with MaybeResult',
- args: { input: z.string().describe('Input string') },
- result: { output: z.string().describe('Output string') },
- throws: ['ERROR_1', 'ERROR_2'],
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result.maybeResultFn.returns).toEqual({
- type: SchemaType.OBJECT,
- properties: {
- output: {
- type: SchemaType.STRING,
- description: 'Output string',
- },
- },
- required: ['output'],
- });
-
- expect(result.maybeResultFn.parameters).toEqual({
- type: SchemaType.OBJECT,
- properties: {
- input: {
- type: SchemaType.STRING,
- description: 'Input string',
- },
- },
- required: ['input'],
- });
- });
-});
-
-describe('Modifies flag', () => {
- test('include modifies flag when present', () => {
- const svc = new Svc('Modifies Service').fn('modifyingFn', {
- description: 'Function that modifies data',
- args: { input: z.string() },
- modifies: true,
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result.modifyingFn.modifies).toBe(true);
- });
-
- test('exclude modifies flag when not present', () => {
- const svc = new Svc('Non-modifying Service').fn('nonModifyingFn', {
- description: 'Function that does not modify data',
- args: { input: z.string() },
- });
-
- const result = convertSvcToVertexAI(svc);
-
- expect(result.nonModifyingFn).not.toHaveProperty('modifies');
- });
-});
diff --git a/js/app/packages/core/util/withValidation.spec.ts b/js/app/packages/core/util/withValidation.spec.ts
deleted file mode 100644
index c4f6a2df09..0000000000
--- a/js/app/packages/core/util/withValidation.spec.ts
+++ /dev/null
@@ -1,103 +0,0 @@
-import { expect, test, vi } from 'vitest';
-import { z } from 'zod';
-import { type ServiceClient, Svc } from '../service';
-import { err, ok } from './maybeResult';
-import { withValidation } from './withValidation';
-
-// Define a sample service for testing
-const sampleService = new Svc('Sample service')
- .err('notFound', { description: 'Resource not found' })
- .fn('getUser', {
- description: 'Get user by id',
- args: { id: z.number().int().positive() },
- result: { name: z.string(), age: z.number() },
- throws: ['notFound'],
- })
- .fn('createUser', {
- description: 'Create a new user',
- args: { name: z.string(), age: z.number().int().positive() },
- result: { id: z.number() },
- });
-
-// Define a sample client implementation
-const sampleClient: ServiceClient<typeof sampleService> = {
- getUser: vi.fn(async ({ id }) => {
- if (id === 1) {
- return ok({ name: 'John Doe', age: 30 });
- }
- return err('notFound', 'User not found');
- }) as any, // mock screws up the type
- createUser: vi.fn(async () => {
- return { id: 123 } as const;
- }),
-};
-
-const validatedClient = withValidation(sampleService, sampleClient);
-
-test('pass valid input and return correct result', async () => {
- const [errors, result] = await validatedClient.getUser({ id: 1 });
- expect(errors).toBeNull();
- expect(result).toEqual({ name: 'John Doe', age: 30 });
-});
-
-test('return service-defined error for invalid resource', async () => {
- const [errors, result] = await validatedClient.getUser({ id: 999 });
- expect(errors).toEqual([{ code: 'notFound', message: 'User not found' }]);
- expect(result).toBeNull();
-});
-
-test('return "invalid" error for invalid input type', async () => {
- const [errors, result] = await validatedClient.getUser({
- id: 'not-a-number',
- } as any);
- expect(errors).toHaveLength(1);
- expect(errors?.[0].code).toBe('invalid');
- expect(errors?.[0].arg).toBe('id');
- expect(result).toBeNull();
-});
-
-test('return "invalid" error for out-of-range input', async () => {
- const [errors, result] = await validatedClient.getUser({ id: -1 });
- expect(errors).toHaveLength(1);
- expect(errors?.[0].code).toBe('invalid');
- expect(errors?.[0].arg).toBe('id');
- expect(result).toBeNull();
-});
-
-test('pass valid input for function without "throws"', async () => {
- const [errors, result] = await validatedClient.createUser({
- name: 'Jane Doe',
- age: 25,
- });
- expect(errors).toBeNull();
- expect(result).toEqual({ id: 123 });
-});
-
-test('return "invalid" error for invalid input in function without "throws"', async () => {
- const [errors, result] = await validatedClient.createUser({
- name: 'Jane Doe',
- age: -5,
- });
- expect(errors).toHaveLength(1);
- expect(errors?.[0].code).toBe('invalid');
- expect(errors?.[0].arg).toBe('age');
- expect(result).toBeNull();
-});
-
-test('handle unexpected errors and return them as "invalid"', async () => {
- const errorClient = {
- getUser: vi.fn(async () => {
- throw new Error('Unexpected error');
- }),
- };
- const errorValidatedClient = withValidation(
- sampleService,
- errorClient as any
- );
-
- const [errors, result] = await errorValidatedClient.getUser({ id: 1 });
- expect(errors).toHaveLength(1);
- expect(errors?.[0].code).toBe('invalid');
- expect(errors?.[0].message).toBe('Unexpected error');
- expect(result).toBeNull();
-});