note-state-id.test.mjs
54 lines 1.7 KB
Raw
sha256:8d46372e39d2d5a54fd93a8b1c27922fe0d9b22a72197345f1d2c71701cc4ce2 feat(auth): persistent login system + C7 session introspection Human minor ⚠ breaking 16 days ago
1 /**
2 * @import { test } from 'node:test';
3 * @import assert from 'node:assert/strict';
4 */
5 import { describe, it } from 'node:test';
6 import assert from 'node:assert/strict';
7 import {
8 absentNoteStateId,
9 fnv1a64Hex,
10 noteStateIdFromParts,
11 noteStateIdFromRawStrings,
12 noteStateIdFromHubNoteJson,
13 stableStringify,
14 } from '../lib/note-state-id.mjs';
15
16 describe('note-state-id', () => {
17 it('stableStringify sorts object keys', () => {
18 assert.equal(stableStringify({ b: 1, a: 2 }), '{"a":2,"b":1}');
19 });
20
21 it('absentNoteStateId is stable kn1_ prefix', () => {
22 const a = absentNoteStateId();
23 assert.ok(a.startsWith('kn1_'));
24 assert.equal(a.length, 4 + 16);
25 });
26
27 it('noteStateIdFromParts is deterministic', () => {
28 const x = noteStateIdFromParts({ title: 'Hi' }, 'body');
29 const y = noteStateIdFromParts({ title: 'Hi' }, 'body');
30 assert.equal(x, y);
31 });
32
33 it('noteStateIdFromRawStrings differs from reordered JSON object hash', () => {
34 const raw = '{"b":1,"a":2}';
35 const fromRaw = noteStateIdFromRawStrings(raw, 'x');
36 const fromObj = noteStateIdFromParts({ b: 1, a: 2 }, 'x');
37 assert.notEqual(fromRaw, fromObj);
38 });
39
40 it('noteStateIdFromHubNoteJson handles object frontmatter', () => {
41 const id = noteStateIdFromHubNoteJson({ frontmatter: { z: 1 }, body: 'b' });
42 assert.ok(id.startsWith('kn1_'));
43 });
44
45 it('noteStateIdFromHubNoteJson handles string frontmatter', () => {
46 const id = noteStateIdFromHubNoteJson({ frontmatter: '{"t":1}', body: 'b' });
47 assert.ok(id.startsWith('kn1_'));
48 });
49
50 it('fnv1a64Hex matches single zero byte', () => {
51 const h = fnv1a64Hex(Buffer.from([0x00]));
52 assert.equal(h.length, 16);
53 });
54 });
File History 2 commits
sha256:8d46372e39d2d5a54fd93a8b1c27922fe0d9b22a72197345f1d2c71701cc4ce2 feat(auth): persistent login system + C7 session introspection Human minor 16 days ago