Skip to content

Commit

Permalink
fix+regress
Browse files Browse the repository at this point in the history
jacoobes committed Jan 13, 2025

Verified

This commit was signed with the committer’s verified signature.
Kikobeats Kiko Beats
1 parent 8073b32 commit bfe8d1d
Showing 8 changed files with 53 additions and 17 deletions.
4 changes: 0 additions & 4 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
@@ -38,19 +38,15 @@ export async function callInitPlugins(_module: Module, deps: Dependencies, emit?
}

export function executeModule(emitter: Emitter, { module, args } : ExecutePayload) {
//do not await. this will block sern

const moduleCalled = wrapAsync(async () => {
console.log(module)
return module.execute(...args);
})
moduleCalled
.then(() => {
console.log('success execute')
emitter.emit('module.activate', resultPayload('success', module))
})
.catch(err => {
console.log('err', err)
if(!emitter.emit('error', resultPayload('failure', module, err))) {
console.error(err)
}
9 changes: 3 additions & 6 deletions src/handlers/interaction.ts
Original file line number Diff line number Diff line change
@@ -30,16 +30,13 @@ export function interactionHandler(deps: UnpackedDependencies, defaultPrefix?: s
let payload;
if(isAutocomplete(event)) {
//@ts-ignore stfu
const option = treeSearch(event, module.options);
//@ts-ignore stfu
const { command } = option;
const { command } = treeSearch(event, module.options);
payload= { module: command as Module, //autocomplete is not a true "module" warning cast!
args: [event, createSDT(command, deps, params)] };
} else if(isCommand(event)) {
payload={ module,
args: [Context.wrap(event, defaultPrefix), createSDT(module, deps, params)] };
payload= { module, args: [Context.wrap(event, defaultPrefix), createSDT(module, deps, params)] };
} else if (isModal(event) || isMessageComponent(event)) {
payload={ module, args: [event, createSDT(module, deps, params)] }
payload= { module, args: [event, createSDT(module, deps, params)] }
} else {
throw Error("Unknown interaction while handling in interactionCreate event " + event)
}
1 change: 1 addition & 0 deletions src/handlers/message.ts
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ export function messageHandler (deps: UnpackedDependencies, defaultPrefix?: stri
emitter.emit('module.activate', resultPayload('failure', module, result.error ?? SernError.PluginFailure))
return
}

//@ts-ignore
payload.args[1].state = result.value

4 changes: 2 additions & 2 deletions src/handlers/presence.ts
Original file line number Diff line number Diff line change
@@ -80,9 +80,9 @@ const parseConfig = async (conf: Promise<Presence.Result>, setPresence: SetPrese
});
}
}

// No repeat configuration, just return the result
return result;
return setPresence(result);
};

export const presenceHandler = async (path: string, setPresence: SetPresence) => {
2 changes: 1 addition & 1 deletion src/handlers/ready.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { once } from 'node:events';
import { resultPayload } from '../core/functions';
import { CommandType } from '../core/structures/enums';
import { Module } from '../types/core-modules';
import { UnpackedDependencies } from '../types/utility';
import type { UnpackedDependencies } from '../types/utility';
import { callInitPlugins } from './event-utils';

export default async function(dir: string, deps : UnpackedDependencies) {
3 changes: 1 addition & 2 deletions src/handlers/user-defined-events.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import type { UnpackedDependencies } from '../types/utility';
import type { Emitter } from '../core/interfaces';
import { inspect } from 'util'
import { resultPayload } from '../core/functions';
import { Wrapper } from '../'
import type { Wrapper } from '../'

export default async function(deps: UnpackedDependencies, wrapper: Wrapper) {
const eventModules: EventModule[] = [];
@@ -52,7 +52,6 @@ export default async function(deps: UnpackedDependencies, wrapper: Wrapper) {
logger?.error({ message: inspect(err) });
}
}

}
source.addListener(String(module.name!), execute)
}
39 changes: 37 additions & 2 deletions test/core/presence.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Presence } from '../../src';

import * as Files from '../../src/core/module-loading'
import { presenceHandler } from '../../src/handlers/presence'

// Example test suite for the module function
describe('module function', () => {
@@ -54,4 +55,38 @@ describe('of function', () => {
activities: [{ name: 'Another Test Activity' }],
});
});


})


describe('Presence module execution', () => {
const mockExecuteResult = Presence.of({
status: 'online',
}).once();

const mockModule = Presence.module({
inject: [ '@sern/client'],
execute: vi.fn().mockReturnValue(mockExecuteResult)
})
beforeEach(() => {
vi.clearAllMocks();
// Mock Files.importModule
vi.spyOn(Files, 'importModule').mockResolvedValue({
module: mockModule
});



});
it('should set presence once.', async () => {
const setPresenceMock = vi.fn();
const mockPath = '/path/to/presence/config';

await presenceHandler(mockPath, setPresenceMock);

expect(Files.importModule).toHaveBeenCalledWith(mockPath);
expect(setPresenceMock).toHaveBeenCalledOnce();
})

})
8 changes: 8 additions & 0 deletions test/setup/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { vi } from 'vitest'
import { makeDependencies } from '../../src';
import { Client } from 'discord.js';

vi.mock('discord.js', async (importOriginal) => {
const mod = await importOriginal()
@@ -44,3 +46,9 @@ vi.mock('discord.js', async (importOriginal) => {
ChatInputCommandInteraction: vi.fn()
};
});

await makeDependencies(({ add }) => {
add('@sern/client', { })
})


0 comments on commit bfe8d1d

Please sign in to comment.