Skip to main content
Glama

contentful-mcp

entry-handler-merge.test.ts4.59 kB
import { expect, vi, describe, it, beforeEach } from "vitest" import { entryHandlers } from "../../src/handlers/entry-handlers.js" // Define test constants const TEST_ENTRY_ID = "test-entry-id" const TEST_SPACE_ID = "test-space-id" const TEST_ENV_ID = "master" // Move vi.mock call to top level - this gets hoisted automatically vi.mock("../../src/config/client.js", () => { return { getContentfulClient: vi.fn().mockImplementation(() => { // Create mock entry when the function is called const mockEntry = { sys: { id: TEST_ENTRY_ID, version: 1 }, fields: { title: { "en-US": "Original Title" }, description: { "en-US": "Original Description" }, tags: { "en-US": ["tag1", "tag2"] } } } return { entry: { get: vi.fn().mockResolvedValue(mockEntry), update: vi.fn().mockImplementation((params, entryProps) => { // Return a merged entry that simulates the updated fields return Promise.resolve({ sys: { id: params.entryId, version: 2 }, fields: entryProps.fields }) }) } } }) } }) describe("Entry Handler Merge Logic", () => { beforeEach(() => { vi.clearAllMocks() }) it("should merge existing fields with update fields when only partial update is provided", async () => { // Setup - just update the title but not other fields const updateData = { spaceId: TEST_SPACE_ID, environmentId: TEST_ENV_ID, entryId: TEST_ENTRY_ID, fields: { title: { "en-US": "Updated Title" } } } // Execute const result = await entryHandlers.updateEntry(updateData) // Parse the result const updatedEntry = JSON.parse(result.content[0].text) // Assert - should have updated title but kept original description and tags expect(updatedEntry.fields.title["en-US"]).toEqual("Updated Title") expect(updatedEntry.fields.description["en-US"]).toEqual("Original Description") expect(updatedEntry.fields.tags["en-US"]).toEqual(["tag1", "tag2"]) }) it("should handle updates to nested locale fields", async () => { // Setup - update a specific locale but not others const updateData = { spaceId: TEST_SPACE_ID, environmentId: TEST_ENV_ID, entryId: TEST_ENTRY_ID, fields: { title: { "de-DE": "Deutscher Titel" // Add a new locale } } } // Execute const result = await entryHandlers.updateEntry(updateData) // Parse the result const updatedEntry = JSON.parse(result.content[0].text) // Assert - should merge the locales in the title field expect(updatedEntry.fields.title["en-US"]).toEqual("Original Title") // Kept original locale expect(updatedEntry.fields.title["de-DE"]).toEqual("Deutscher Titel") // Added new locale expect(updatedEntry.fields.description["en-US"]).toEqual("Original Description") // Kept other fields }) it("should handle adding a new field", async () => { // Setup - add a completely new field const updateData = { spaceId: TEST_SPACE_ID, environmentId: TEST_ENV_ID, entryId: TEST_ENTRY_ID, fields: { newField: { "en-US": "New Field Value" } } } // Execute const result = await entryHandlers.updateEntry(updateData) // Parse the result const updatedEntry = JSON.parse(result.content[0].text) // Assert - should have original fields plus the new field expect(updatedEntry.fields.title["en-US"]).toEqual("Original Title") expect(updatedEntry.fields.description["en-US"]).toEqual("Original Description") expect(updatedEntry.fields.newField["en-US"]).toEqual("New Field Value") }) it("should handle updating an array field", async () => { // Setup - update the tags array const updateData = { spaceId: TEST_SPACE_ID, environmentId: TEST_ENV_ID, entryId: TEST_ENTRY_ID, fields: { tags: { "en-US": ["tag1", "tag2", "tag3"] } // Add tag3 } } // Execute const result = await entryHandlers.updateEntry(updateData) // Parse the result const updatedEntry = JSON.parse(result.content[0].text) // Assert - should have updated tags but kept other fields expect(updatedEntry.fields.title["en-US"]).toEqual("Original Title") expect(updatedEntry.fields.description["en-US"]).toEqual("Original Description") expect(updatedEntry.fields.tags["en-US"]).toEqual(["tag1", "tag2", "tag3"]) }) })

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ivo-toby/contentful-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server