diff --git a/packages/misskey-js/src/acct.ts b/packages/misskey-js/src/acct.ts index aa8658cdbd..469cc35b14 100644 --- a/packages/misskey-js/src/acct.ts +++ b/packages/misskey-js/src/acct.ts @@ -6,6 +6,7 @@ export type Acct = { export function parse(_acct: string): Acct { let acct = _acct; if (acct.startsWith('@')) acct = acct.substring(1); + else if (acct.startsWith('acct:')) acct = acct.substring(5); const split = acct.split('@', 2); return { username: split[0], host: split[1] || null }; } diff --git a/packages/misskey-js/test/acct.ts b/packages/misskey-js/test/acct.ts new file mode 100644 index 0000000000..6db8aea58b --- /dev/null +++ b/packages/misskey-js/test/acct.ts @@ -0,0 +1,35 @@ +import { describe, it, expect } from 'vitest'; +import * as acct from '../src/acct.js'; + +describe('acct.parse', () => { + it('parses plain username', () => { + const res = acct.parse('alice'); + expect(res).toEqual({ username: 'alice', host: null }); + }); + + it('parses at-mark style without host', () => { + const res = acct.parse('@alice'); + expect(res).toEqual({ username: 'alice', host: null }); + }); + + it('parses at-mark style with host', () => { + const res = acct.parse('@alice@example.com'); + expect(res).toEqual({ username: 'alice', host: 'example.com' }); + }); + + it('parses acct: style', () => { + const res = acct.parse('acct:alice@example.com'); + expect(res).toEqual({ username: 'alice', host: 'example.com' }); + }); +}); + +describe('acct.toString', () => { + it('returns username when host is null', () => { + expect(acct.toString({ username: 'alice', host: null })).toBe('alice'); + }); + + it('returns username@host when host exists', () => { + expect(acct.toString({ username: 'alice', host: 'example.com' })).toBe('alice@example.com'); + }); +}); +