From 1e88a32bdfe33d144263692e00cbcd1db88bbda6 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 30 Aug 2025 23:41:21 +0900 Subject: [PATCH] =?UTF-8?q?enhance(misskey-js):=20acct.parse()=E3=81=8Cacc?= =?UTF-8?q?t:=E3=81=8B=E3=82=89=E3=81=AF=E3=81=98=E3=81=BE=E3=82=8B?= =?UTF-8?q?=E3=82=82=E3=81=AE=E3=82=82=E3=83=91=E3=83=BC=E3=82=B9=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=20*=20acct.parse()?= =?UTF-8?q?=E3=81=8Cacct:=E3=81=8B=E3=82=89=E5=A7=8B=E3=81=BE=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=A6=E3=82=82=E3=83=91=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=20*=20ac?= =?UTF-8?q?ct.ts=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/misskey-js/src/acct.ts | 1 + packages/misskey-js/test/acct.ts | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 packages/misskey-js/test/acct.ts 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'); + }); +}); +