ReviewOS

also looking at this

stacks/analyticshq

Harden AnalyticsHQ release and dashboard quality

#52
Merged glennmichael123 wants to merge docs/bunpress-product-guide into main
34 files +1236 -340
tests/unit/event-properties.test.tsadded+67-0
Changes to tests/unit/event-properties.test.ts
@@ -0,0 +1,67 @@
1import { describe, expect, test } from 'bun:test'
2import { readFileSync } from 'node:fs'
3import { join } from 'node:path'
4import { MAX_EVENT_PROPERTIES_BYTES, serializeEventProperties } from '../../app/Analytics/event-properties'
5
6describe('event property serialization', () => {
7 test('preserves structured metadata well beyond the old 255-character column', () => {
8 const properties = {
9 campaign: 'spring-launch',
10 notes: 'measured-context-'.repeat(700),
11 }
12 const serialized = serializeEventProperties('Signup', properties)
13
14 expect(serialized).toBe(JSON.stringify(properties))
15 expect(serialized!.length).toBeGreaterThan(10_000)
16 })
17
18 test('keeps the complete URL for reserved automatic events', () => {
19 const url = `https://example.com/${'deep/path/'.repeat(900)}?source=campaign`
20 const serialized = serializeEventProperties('Outbound Link', {
21 ignored: 'does not split the aggregate',
22 url,
23 })
24
25 expect(serialized).toBe(JSON.stringify({ url }))
26 expect(JSON.parse(serialized!).url).toBe(url)
27 })
28
29 test('canonicalizes file downloads the same way', () => {
30 expect(serializeEventProperties('File Download', {
31 url: 'https://example.com/report.csv',
32 arbitrary: true,
33 })).toBe('{"url":"https://example.com/report.csv"}')
34 })
35
36 test('omits optional properties that exceed the UTF-8 byte budget', () => {
37 const properties = { notes: 'é'.repeat(MAX_EVENT_PROPERTIES_BYTES) }
38
39 expect(serializeEventProperties('Oversized', properties)).toBeNull()
40 })
41
42 test('omits values JSON cannot serialize', () => {
43 expect(serializeEventProperties('Invalid', 1n)).toBeNull()
44 })
45})
46
47describe('event property storage', () => {
48 const root = join(import.meta.dir, '..', '..')
49
50 test('upgrades existing PostgreSQL columns to text', () => {
51 const migration = readFileSync(
52 join(root, 'database/migrations/0000000052-widen-custom-event-properties.sql'),
53 'utf8',
54 )
55
56 expect(migration).toContain('ALTER COLUMN "properties" TYPE text')
57 expect(migration).toContain('USING "properties"::text')
58 })
59
60 test('the collector uses the bounded serializer without URL clipping', () => {
61 const source = readFileSync(join(root, 'routes/analytics.ts'), 'utf8')
62
63 expect(source).toContain('serializeEventProperties(event, body.p)')
64 expect(source).not.toContain('while (props.length > 255')
65 expect(source).not.toContain('widen custom_events.properties')
66 })
67})