This commit is contained in:
tamaina 2025-09-21 15:56:01 +09:00 committed by GitHub
commit a8d3539b35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View File

@ -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 };
}

View File

@ -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');
});
});