Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/tools/assets/mockClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export const mockAsset = {
},
},
},
metadata: {
tags: [],
concepts: [],
},
};

/**
Expand Down Expand Up @@ -170,3 +174,57 @@ export const mockProcessedAsset = {
},
},
};

/**
* Mock taxonomy concepts for testing
*/
export const mockTaxonomyConcepts = {
concept1: {
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'concept-1',
},
},
concept2: {
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'concept-2',
},
},
existingConcept: {
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'existing-concept',
},
},
};

/**
* Mock tags for testing
*/
export const mockTags = {
tag1: {
sys: {
type: 'Link' as const,
linkType: 'Tag' as const,
id: 'tag-1',
},
},
tag2: {
sys: {
type: 'Link' as const,
linkType: 'Tag' as const,
id: 'tag-2',
},
},
existingTag: {
sys: {
type: 'Link' as const,
linkType: 'Tag' as const,
id: 'existing-tag',
},
},
};
204 changes: 204 additions & 0 deletions src/tools/assets/updateAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('updateAsset', () => {
},
metadata: {
tags: [],
concepts: [],
},
},
);
Expand All @@ -94,6 +95,7 @@ describe('updateAsset', () => {
},
},
],
concepts: [],
},
};

Expand All @@ -109,6 +111,7 @@ describe('updateAsset', () => {
},
},
],
concepts: [],
},
};

Expand All @@ -135,6 +138,7 @@ describe('updateAsset', () => {
},
},
],
concepts: [],
},
};

Expand Down Expand Up @@ -163,6 +167,7 @@ describe('updateAsset', () => {
},
},
],
concepts: [],
},
}),
);
Expand Down Expand Up @@ -278,6 +283,205 @@ describe('updateAsset', () => {
expect.objectContaining({
metadata: {
tags: [],
concepts: [],
},
}),
);
});

it('should update an asset with new taxonomy concepts', async () => {
const testArgs = {
...mockArgs,
fields: {
title: { 'en-US': 'Asset with Concepts' },
},
metadata: {
tags: [],
concepts: [
{
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'concept-1',
},
},
],
},
};

const assetWithExistingConcepts = {
...mockAsset,
metadata: {
tags: [],
concepts: [
{
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'existing-concept',
},
},
],
},
};

const updatedAsset = {
...assetWithExistingConcepts,
fields: {
...assetWithExistingConcepts.fields,
title: { 'en-US': 'Asset with Concepts' },
},
metadata: {
tags: [],
concepts: [
{
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'existing-concept',
},
},
{
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'concept-1',
},
},
],
},
};

mockAssetGet.mockResolvedValue(assetWithExistingConcepts);
mockAssetUpdate.mockResolvedValue(updatedAsset);

await updateAssetTool(testArgs);

expect(mockAssetUpdate).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
metadata: {
tags: [],
concepts: [
{
sys: {
type: 'Link',
linkType: 'TaxonomyConcept',
id: 'existing-concept',
},
},
{
sys: {
type: 'Link',
linkType: 'TaxonomyConcept',
id: 'concept-1',
},
},
],
},
}),
);
});

it('should update an asset with both tags and concepts', async () => {
const testArgs = {
...mockArgs,
fields: {
title: { 'en-US': 'Asset with Tags and Concepts' },
},
metadata: {
tags: [
{
sys: {
type: 'Link' as const,
linkType: 'Tag' as const,
id: 'new-tag',
},
},
],
concepts: [
{
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'new-concept',
},
},
],
},
};

const assetWithExistingMetadata = {
...mockAsset,
metadata: {
tags: [
{
sys: {
type: 'Link' as const,
linkType: 'Tag' as const,
id: 'existing-tag',
},
},
],
concepts: [
{
sys: {
type: 'Link' as const,
linkType: 'TaxonomyConcept' as const,
id: 'existing-concept',
},
},
],
},
};

mockAssetGet.mockResolvedValue(assetWithExistingMetadata);
mockAssetUpdate.mockResolvedValue({
...assetWithExistingMetadata,
fields: {
...assetWithExistingMetadata.fields,
title: { 'en-US': 'Asset with Tags and Concepts' },
},
});

await updateAssetTool(testArgs);

expect(mockAssetUpdate).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
metadata: {
tags: [
{
sys: {
type: 'Link',
linkType: 'Tag',
id: 'existing-tag',
},
},
{
sys: {
type: 'Link',
linkType: 'Tag',
id: 'new-tag',
},
},
],
concepts: [
{
sys: {
type: 'Link',
linkType: 'TaxonomyConcept',
id: 'existing-concept',
},
},
{
sys: {
type: 'Link',
linkType: 'TaxonomyConcept',
id: 'new-concept',
},
},
],
},
}),
);
Expand Down
32 changes: 15 additions & 17 deletions src/tools/assets/updateAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
withErrorHandling,
} from '../../utils/response.js';
import { BaseToolSchema, createToolClient } from '../../utils/tools.js';
import { AssetMetadataSchema } from '../../types/taxonomySchema.js';

export const UpdateAssetToolParams = BaseToolSchema.extend({
assetId: z.string().describe('The ID of the asset to update'),
Expand All @@ -12,19 +13,7 @@ export const UpdateAssetToolParams = BaseToolSchema.extend({
.describe(
'The field values to update. Keys should be field IDs and values should be the field content. Will be merged with existing fields.',
),
metadata: z
.object({
tags: z.array(
z.object({
sys: z.object({
type: z.literal('Link'),
linkType: z.literal('Tag'),
id: z.string(),
}),
}),
),
})
.optional(),
metadata: AssetMetadataSchema,
});

type Params = z.infer<typeof UpdateAssetToolParams>;
Expand All @@ -40,14 +29,23 @@ async function tool(args: Params) {

// Get existing asset, merge fields, and update
const existingAsset = await contentfulClient.asset.get(params);

const allTags = [
...(existingAsset.metadata?.tags || []),
...(args.metadata?.tags || []),
];

const allConcepts = [
...(existingAsset.metadata?.concepts || []),
...(args.metadata?.concepts || []),
];

const updatedAsset = await contentfulClient.asset.update(params, {
...existingAsset,
fields: { ...existingAsset.fields, ...args.fields },
metadata: {
tags: [
...(existingAsset.metadata?.tags || []),
...(args.metadata?.tags || []),
],
tags: allTags,
concepts: allConcepts,
},
});

Expand Down
Loading