From 621cd12ce66d110f0db7777efd0d4c9282d38295 Mon Sep 17 00:00:00 2001 From: John Smith Date: Tue, 10 Dec 2024 19:03:08 +1030 Subject: [PATCH] chore: Add tests for validateServiceRegistration --- .../src/modules/service-definitions.test.ts | 92 ++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/control-plane/src/modules/service-definitions.test.ts b/control-plane/src/modules/service-definitions.test.ts index 8e75bba1..3109d8f7 100644 --- a/control-plane/src/modules/service-definitions.test.ts +++ b/control-plane/src/modules/service-definitions.test.ts @@ -1,5 +1,5 @@ import { dereferenceSync, JSONSchema } from "dereference-json-schema"; -import { InvalidJobArgumentsError } from "../utilities/errors"; +import { InvalidJobArgumentsError, InvalidServiceRegistrationError } from "../utilities/errors"; import { packer } from "./packer"; import { deserializeFunctionSchema, @@ -7,9 +7,11 @@ import { parseJobArgs, serviceFunctionEmbeddingId, updateServiceEmbeddings, + validateServiceRegistration, } from "./service-definitions"; import { createOwner } from "./test/util"; import { zodToJsonSchema } from "zod-to-json-schema"; +import { z } from "zod"; describe("updateServiceEmbeddings", () => { let owner: { clusterId: string }; beforeAll(async () => { @@ -281,3 +283,91 @@ describe("deserializeFunctionSchema", () => { }); }); }); + +describe("validateServiceRegistration", () => { + it("should reject invalid schema", () => { + expect(() => { + validateServiceRegistration({ + service: "default", + definition: { + name: "default", + functions: [ + { + name: "someFn", + schema: JSON.stringify({ + type: "wrong_type", + }) + }, + ], + }, + }); + }).toThrow(InvalidServiceRegistrationError); + }) + + it("should accept valid schema", () => { + expect(() => { + validateServiceRegistration({ + service: "default", + definition: { + name: "default", + functions: [ + { + name: "someFn", + description: "someFn", + schema: JSON.stringify(zodToJsonSchema( + z.object({ + test: z.string(), + }) + )) + }, + ], + }, + }); + }).not.toThrow(); + }) + + it("should reject incorrect handleCustomerAuth registration", () => { + expect(() => { + validateServiceRegistration({ + service: "default", + definition: { + name: "default", + functions: [ + { + name: "handleCustomerAuth", + description: "handleCustomerAuth", + schema: JSON.stringify(zodToJsonSchema( + z.object({ + test: z.string(), + }) + )) + }, + ], + }, + }); + }).toThrow(InvalidServiceRegistrationError); + }) + + it("should accept valid handleCustomerAuth registration", () => { + expect(() => { + validateServiceRegistration({ + service: "default", + definition: { + name: "default", + functions: [ + { + name: "handleCustomerAuth", + description: "handleCustomerAuth", + schema: JSON.stringify(zodToJsonSchema( + z.object({ + token: z.string(), + }) + )) + }, + ], + }, + }); + }).not.toThrow(); + }) + +})