1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-21 18:18:15 +09:00
MisskeyIO/packages/backend/test/unit/misc/id.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { ulid } from 'ulid';
import { describe, test, expect } from '@jest/globals';
import { aidRegExp, genAid, parseAid } from '@/misc/id/aid.js';
import { aidxRegExp, genAidx, parseAidx } from '@/misc/id/aidx.js';
import { genMeid, meidRegExp, parseMeid } from '@/misc/id/meid.js';
import { genMeidg, meidgRegExp, parseMeidg } from '@/misc/id/meidg.js';
import { genObjectId, objectIdRegExp, parseObjectId } from '@/misc/id/object-id.js';
import { ulidRegExp, parseUlid } from '@/misc/id/ulid.js';
describe('misc:id', () => {
test('aid', () => {
2023-10-16 12:58:17 +09:00
const date = Date.now();
const gotAid = genAid(date);
expect(gotAid).toMatch(aidRegExp);
2023-10-16 12:58:17 +09:00
expect(parseAid(gotAid).date.getTime()).toBe(date);
});
test('aidx', () => {
2023-10-16 12:58:17 +09:00
const date = Date.now();
const gotAidx = genAidx(date);
expect(gotAidx).toMatch(aidxRegExp);
2023-10-16 12:58:17 +09:00
expect(parseAidx(gotAidx).date.getTime()).toBe(date);
});
test('meid', () => {
2023-10-16 12:58:17 +09:00
const date = Date.now();
const gotMeid = genMeid(date);
expect(gotMeid).toMatch(meidRegExp);
2023-10-16 12:58:17 +09:00
expect(parseMeid(gotMeid).date.getTime()).toBe(date);
});
test('meidg', () => {
2023-10-16 12:58:17 +09:00
const date = Date.now();
const gotMeidg = genMeidg(date);
expect(gotMeidg).toMatch(meidgRegExp);
2023-10-16 12:58:17 +09:00
expect(parseMeidg(gotMeidg).date.getTime()).toBe(date);
});
test('objectid', () => {
2023-10-16 12:58:17 +09:00
const date = Date.now();
const gotObjectId = genObjectId(date);
expect(gotObjectId).toMatch(objectIdRegExp);
2023-10-16 12:58:17 +09:00
expect(Math.floor(parseObjectId(gotObjectId).date.getTime() / 1000)).toBe(Math.floor(date / 1000));
});
test('ulid', () => {
2023-10-16 12:58:17 +09:00
const date = Date.now();
const gotUlid = ulid(date);
expect(gotUlid).toMatch(ulidRegExp);
2023-10-16 12:58:17 +09:00
expect(parseUlid(gotUlid).date.getTime()).toBe(date);
});
});