Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(auto-tagging): add multi-provider integration tests (#1322)

Corentin Thomassetb330216bb849
diff --git a/apps/papra-server/.env.test.example b/apps/papra-server/.env.test.example
index d492db6b..8e8951e5 100644
--- a/apps/papra-server/.env.test.example
+++ b/apps/papra-server/.env.test.example
@@ -7,3 +7,15 @@ TEST_B2_ENDPOINT=
TEST_B2_BUCKET_NAME=
TEST_B2_ACCESS_KEY_ID=
TEST_B2_SECRET_ACCESS_KEY=
+
+TEST_OLLAMA_BASE_URL=http://localhost:11434/v1
+TEST_OLLAMA_MODEL_ID=ollama://qwen3:8b
+
+TEST_ANTHROPIC_API_KEY=
+TEST_ANTHROPIC_MODEL_ID=anthropic://claude-haiku-4-5
+
+TEST_MISTRAL_API_KEY=
+TEST_MISTRAL_MODEL_ID=mistral://mistral-medium-3-5
+
+TEST_OPENAI_API_KEY=
+TEST_OPENAI_MODEL_ID=openai://gpt-5.4-mini
diff --git a/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.ts b/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.ts
index 50a9b6b1..32c54b38 100644
--- a/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.ts
+++ b/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.ts
@@ -18,6 +18,36 @@ import {
} from './auto-tagging.models';
import { ensureModelId } from '../ai/ai.models';
+export async function promptForAutoTagging({
+ aiServices,
+ document,
+ existingTags,
+ canCreateNewTags,
+ maxTags,
+ modelId,
+}: {
+ aiServices: AiServices;
+ document: { content: string; name: string };
+ existingTags: { id: string; name: string; description?: string | null }[];
+ canCreateNewTags: boolean;
+ maxTags: number;
+ modelId: string;
+}) {
+ const response = await aiServices.generateStructuredData({
+ modelId,
+ schema: buildAutoTaggingSchema({ existingTags, canCreateNewTags, maxTags }),
+ systemPrompt: buildAutoTaggingSystemPrompt({ existingTags, canCreateNewTags, maxTags }),
+ userPrompt: buildAutoTaggingUserPrompt({ document }),
+ });
+
+ const { tagIdsToAdd, tagsToCreate } = getTagsActions({
+ requestedTags: response,
+ existingTags,
+ });
+
+ return { tagIdsToAdd, tagsToCreate };
+}
+
export async function autoTagDocument({
aiServices,
documentId,
@@ -73,20 +103,16 @@ export async function autoTagDocument({
}
const startedAt = Date.now();
- const response = await aiServices.generateStructuredData({
+ const { tagIdsToAdd, tagsToCreate } = await promptForAutoTagging({
+ aiServices,
+ document,
+ existingTags,
+ canCreateNewTags,
+ maxTags,
modelId: ensureModelId(organizationSettings.ai.autoTagging.modelId),
- schema: buildAutoTaggingSchema({ existingTags, canCreateNewTags, maxTags }),
- systemPrompt: buildAutoTaggingSystemPrompt({ existingTags, canCreateNewTags, maxTags }),
- userPrompt: buildAutoTaggingUserPrompt({ document }),
});
const durationMs = Date.now() - startedAt;
-
- logger.info({ durationMs, documentId, organizationId }, 'Auto-tagging completed');
-
- const { tagIdsToAdd, tagsToCreate } = getTagsActions({
- requestedTags: response,
- existingTags,
- });
+ logger.info({ documentId, organizationId, durationMs }, 'Auto-tagging completed');
const limit = pLimit(5);
diff --git a/apps/papra-server/src/modules/auto-tagging/int-tests/anthropic.auto-tagging.int.test.ts b/apps/papra-server/src/modules/auto-tagging/int-tests/anthropic.auto-tagging.int.test.ts
new file mode 100644
index 00000000..81f83a07
--- /dev/null
+++ b/apps/papra-server/src/modules/auto-tagging/int-tests/anthropic.auto-tagging.int.test.ts
@@ -0,0 +1,15 @@
+import { describe } from 'vitest';
+import { overrideConfig } from '../../config/config.test-utils';
+import { runAutoTaggingTestSuite } from './auto-tagging.int.test-suites';
+
+const apiKey = process.env.TEST_ANTHROPIC_API_KEY;
+const modelId = process.env.TEST_ANTHROPIC_MODEL_ID;
+
+describe('auto-tagging ai integration', () => {
+ describe.skipIf(!apiKey || !modelId)('anthropic', () => {
+ runAutoTaggingTestSuite({
+ modelId: modelId!,
+ config: overrideConfig({ ai: { adapters: { anthropic: { apiKey } } } }),
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/auto-tagging/int-tests/auto-tagging.int.test-suites.ts b/apps/papra-server/src/modules/auto-tagging/int-tests/auto-tagging.int.test-suites.ts
new file mode 100644
index 00000000..3ee2d82b
--- /dev/null
+++ b/apps/papra-server/src/modules/auto-tagging/int-tests/auto-tagging.int.test-suites.ts
@@ -0,0 +1,49 @@
+import { expect, test } from 'vitest';
+import type { Config } from '../../config/config.types';
+import { createAiServices } from '../../ai/ai.services';
+import { promptForAutoTagging } from '../auto-tagging.usecases';
+
+const document = {
+ name: 'acme-invoice-2026-06.pdf',
+ content: [
+ 'INVOICE #2026-042',
+ 'From: Acme Corp, 123 Main St',
+ 'To: John Doe',
+ 'Web hosting services, June 2026',
+ 'Total due: $1,250.00 - payment within 30 days',
+ ].join('\n'),
+};
+
+const existingTags = [
+ { id: 'tag_1', name: 'invoice', description: 'Bills and invoices' },
+ { id: 'tag_2', name: 'recipe', description: 'Cooking recipes' },
+ { id: 'tag_3', name: 'contract', description: null },
+ { id: 'tag_4', name: 'garden', description: 'Gardening and landscaping' },
+];
+
+export function runAutoTaggingTestSuite({
+ modelId,
+ config,
+ timeout = 20_000,
+}: {
+ modelId: string;
+ config: Config;
+ timeout?: number;
+}) {
+ const aiServices = createAiServices({ config });
+
+ test('auto-tagging should correctly identify tags for a document', { timeout }, async () => {
+ const { tagIdsToAdd, tagsToCreate } = await promptForAutoTagging({
+ existingTags,
+ modelId,
+ aiServices,
+ canCreateNewTags: false,
+ document,
+ maxTags: 3,
+ });
+
+ expect(tagsToCreate).toEqual([]);
+ expect(tagIdsToAdd.length).toBeLessThanOrEqual(3);
+ expect(tagIdsToAdd).toContain('tag_1'); // the document is unambiguously an invoice
+ });
+}
diff --git a/apps/papra-server/src/modules/auto-tagging/int-tests/mistral.auto-tagging.int.test.ts b/apps/papra-server/src/modules/auto-tagging/int-tests/mistral.auto-tagging.int.test.ts
new file mode 100644
index 00000000..1be2e4c1
--- /dev/null
+++ b/apps/papra-server/src/modules/auto-tagging/int-tests/mistral.auto-tagging.int.test.ts
@@ -0,0 +1,15 @@
+import { describe } from 'vitest';
+import { overrideConfig } from '../../config/config.test-utils';
+import { runAutoTaggingTestSuite } from './auto-tagging.int.test-suites';
+
+const apiKey = process.env.TEST_MISTRAL_API_KEY;
+const modelId = process.env.TEST_MISTRAL_MODEL_ID;
+
+describe('auto-tagging ai integration', () => {
+ describe.skipIf(!apiKey || !modelId)('mistral', () => {
+ runAutoTaggingTestSuite({
+ modelId: modelId!,
+ config: overrideConfig({ ai: { adapters: { mistral: { apiKey } } } }),
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/auto-tagging/int-tests/ollama.auto-tagging.int.test.ts b/apps/papra-server/src/modules/auto-tagging/int-tests/ollama.auto-tagging.int.test.ts
new file mode 100644
index 00000000..320a7f38
--- /dev/null
+++ b/apps/papra-server/src/modules/auto-tagging/int-tests/ollama.auto-tagging.int.test.ts
@@ -0,0 +1,15 @@
+import { describe } from 'vitest';
+import { overrideConfig } from '../../config/config.test-utils';
+import { runAutoTaggingTestSuite } from './auto-tagging.int.test-suites';
+
+const baseUrl = process.env.TEST_OLLAMA_BASE_URL;
+const modelId = process.env.TEST_OLLAMA_MODEL_ID;
+
+describe('auto-tagging ai integration', () => {
+ describe.skipIf(!baseUrl || !modelId)('ollama', () => {
+ runAutoTaggingTestSuite({
+ modelId: modelId!,
+ config: overrideConfig({ ai: { adapters: { ollama: { baseUrl } } } }),
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/auto-tagging/int-tests/openai.auto-tagging.int.test.ts b/apps/papra-server/src/modules/auto-tagging/int-tests/openai.auto-tagging.int.test.ts
new file mode 100644
index 00000000..01b62611
--- /dev/null
+++ b/apps/papra-server/src/modules/auto-tagging/int-tests/openai.auto-tagging.int.test.ts
@@ -0,0 +1,15 @@
+import { describe } from 'vitest';
+import { overrideConfig } from '../../config/config.test-utils';
+import { runAutoTaggingTestSuite } from './auto-tagging.int.test-suites';
+
+const apiKey = process.env.TEST_OPENAI_API_KEY;
+const modelId = process.env.TEST_OPENAI_MODEL_ID;
+
+describe('auto-tagging ai integration', () => {
+ describe.skipIf(!apiKey || !modelId)('openai', () => {
+ runAutoTaggingTestSuite({
+ modelId: modelId!,
+ config: overrideConfig({ ai: { adapters: { openai: { apiKey } } } }),
+ });
+ });
+});