Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allouis committed Dec 18, 2024
1 parent c240166 commit 6688e5d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ export async function getSiteDataHandler(
// This is to ensure that the actor exists - e.g. for a brand new a site
await getUserData(apCtx, handle);

await updateSiteActor(apCtx, getSiteSettings, host);
await updateSiteActor(apCtx, getSiteSettings);

return new Response(JSON.stringify(site), {
status: 200,
Expand All @@ -595,7 +595,7 @@ export async function siteChangedWebhook(
logger,
});

await updateSiteActor(apCtx, getSiteSettings, host);
await updateSiteActor(apCtx, getSiteSettings);
} catch (err) {
ctx.get('logger').error('Site changed webhook failed: {error}', {
error: err,
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/activitypub/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ export async function updateSiteActor(
getSiteSettings: (host: string) => Promise<{
site: { icon: string; title: string; description: string };
}>,
host: string,
) {
const settings = await getSiteSettings(host);
const settings = await getSiteSettings(apCtx.host);
const handle = ACTOR_DEFAULT_HANDLE;

const current = await getUserData(apCtx, handle);
Expand Down
75 changes: 69 additions & 6 deletions src/helpers/activitypub/actor.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ describe('updateSiteActor', () => {
.fn()
.mockReturnValue(new URL('https://example.com/followers')),
sendActivity: vi.fn(),
host: 'example.com',
} as unknown as RequestContext<ContextData>;
}

Expand All @@ -287,15 +288,17 @@ describe('updateSiteActor', () => {
following: 'https://example.com/following',
followers: 'https://example.com/followers',
liked: 'https://example.com/liked',
url: 'https://example.com',
url: 'https://example.com/',
}),
set: vi.fn(),
} as unknown as KvStore;
delete: vi.fn(),
};

const globaldb = {
get: vi.fn().mockResolvedValue(null),
set: vi.fn(),
} as unknown as KvStore;
delete: vi.fn(),
};

const getSiteSettings = vi.fn().mockResolvedValue({
site: {
Expand All @@ -314,6 +317,50 @@ describe('updateSiteActor', () => {
expect(result).toBe(false);
});

it('should update the site actor if one does not exist', async () => {
const db = {
get: vi.fn().mockResolvedValue(undefined),
set: vi.fn(),
delete: vi.fn(),
};

const globaldb = {
get: vi.fn().mockResolvedValue(null),
set: vi.fn(),
delete: vi.fn(),
};

const getSiteSettings = vi.fn().mockResolvedValue({
site: {
description: 'New Site Description',
title: 'New Site Title',
icon: 'https://example.com/icon.png',
},
});

const host = 'example.com';

const apCtx = mockApContext(db, globaldb);

const result = await updateSiteActor(apCtx, getSiteSettings, host);

expect(result).toBe(true);

expect(db.set.mock.lastCall?.[1]).toStrictEqual({
id: 'https://example.com/user/1',
name: 'New Site Title',
summary: 'New Site Description',
preferredUsername: 'index',
icon: 'https://example.com/icon.png',
inbox: 'https://example.com/inbox',
outbox: 'https://example.com/outbox',
following: 'https://example.com/following',
followers: 'https://example.com/followers',
liked: 'https://example.com/liked',
url: 'https://example.com/',
});
});

it('should update the site actor if the site settings have changed', async () => {
const db = {
get: vi.fn().mockResolvedValue({
Expand All @@ -327,15 +374,17 @@ describe('updateSiteActor', () => {
following: 'https://example.com/following',
followers: 'https://example.com/followers',
liked: 'https://example.com/liked',
url: 'https://example.com',
url: 'https://example.com/',
}),
set: vi.fn(),
} as unknown as KvStore;
delete: vi.fn(),
};

const globaldb = {
get: vi.fn().mockResolvedValue(null),
set: vi.fn(),
} as unknown as KvStore;
delete: vi.fn(),
};

const getSiteSettings = vi.fn().mockResolvedValue({
site: {
Expand All @@ -352,5 +401,19 @@ describe('updateSiteActor', () => {
const result = await updateSiteActor(apCtx, getSiteSettings, host);

expect(result).toBe(true);

expect(db.set.mock.calls[0][1]).toStrictEqual({
id: 'https://example.com/user/1',
name: 'New Site Title',
summary: 'New Site Description',
preferredUsername: 'index',
icon: 'https://example.com/icon.png',
inbox: 'https://example.com/inbox',
outbox: 'https://example.com/outbox',
following: 'https://example.com/following',
followers: 'https://example.com/followers',
liked: 'https://example.com/liked',
url: 'https://example.com/',
});
});
});

0 comments on commit 6688e5d

Please sign in to comment.