testing.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /** Canonical tool-definition fixtures for repository tests. @module dsh-tools/testing */
  2. import type { ContentBlock } from '@deepseek-ai/dsh-llm'
  3. import type { JsonValue } from '@deepseek-ai/dsh-session'
  4. import { defineTool } from './schema.ts'
  5. import type { DefineToolOptions, ParameterSchemaSpec } from './schema.ts'
  6. import type { ToolDefinition, ToolRunContext } from './index.ts'
  7. const CONTENT_VALUE_SCHEMA = { type: 'array', items: { type: 'json' } } as const
  8. /** Options for a fixture whose canonical value is its rendered content array. */
  9. export type ContentToolFixtureOptions<S extends ParameterSchemaSpec> = Omit<
  10. DefineToolOptions<S, typeof CONTENT_VALUE_SCHEMA>,
  11. 'output' | 'execute'
  12. > & {
  13. /** Produce the fixture's content blocks as its canonical test value. */
  14. execute(args: import('./schema.ts').InferArgs<S>, exec: ToolRunContext): Promise<ContentBlock[]>
  15. }
  16. /**
  17. * Define a test fixture that deliberately uses its content blocks as the
  18. * canonical JSON value. Product tools must declare domain-owned DTOs instead.
  19. * @param options - ordinary fixture fields plus a content-producing body.
  20. * @returns a registry-ready tool with an explicit JSON-array output contract.
  21. * @internal
  22. */
  23. export function defineContentToolFixture<const S extends ParameterSchemaSpec>(
  24. options: ContentToolFixtureOptions<S>,
  25. ): ToolDefinition {
  26. // oxlint-disable-next-line typescript/unbound-method
  27. const execute = options.execute
  28. return defineTool({
  29. ...options,
  30. output: {
  31. schema: CONTENT_VALUE_SCHEMA,
  32. render: (_args, value) => value as unknown as ContentBlock[],
  33. },
  34. async execute(args, exec) {
  35. return await execute(args, exec) as unknown as JsonValue[]
  36. },
  37. })
  38. }