/ clis / douyin / _shared / creation-id.test.js
creation-id.test.js
 1  import { describe, expect, it } from 'vitest';
 2  import { generateCreationId } from './creation-id.js';
 3  describe('generateCreationId', () => {
 4      it('starts with "pin"', () => {
 5          expect(generateCreationId()).toMatch(/^pin/);
 6      });
 7      it('has 4 random lowercase-alphanumeric chars after "pin"', () => {
 8          expect(generateCreationId()).toMatch(/^pin[a-z0-9]{4}/);
 9      });
10      it('ends with a numeric timestamp (ms)', () => {
11          const before = Date.now();
12          const id = generateCreationId();
13          const after = Date.now();
14          const ts = parseInt(id.replace(/^pin[a-z0-9]{4}/, ''), 10);
15          expect(ts).toBeGreaterThanOrEqual(before);
16          expect(ts).toBeLessThanOrEqual(after);
17      });
18      it('generates unique IDs', () => {
19          const ids = new Set(Array.from({ length: 100 }, generateCreationId));
20          expect(ids.size).toBe(100);
21      });
22  });