|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { after, beforeEach, test } from 'node:test' |
| 3 | +import { mkdtempSync, rmSync } from 'node:fs' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { tmpdir } from 'node:os' |
| 6 | +import type { OrganizationSnapshot } from '../src/lib/types' |
| 7 | + |
| 8 | +const testDir = mkdtempSync(join(tmpdir(), 'orglabeler-did-label-subjects-')) |
| 9 | +process.env.ACTIVITY_DB_PATH = join(testDir, 'activity-log.db') |
| 10 | +process.env.LABELS_DB_PATH = join(testDir, 'labels.db') |
| 11 | +process.env.DID = 'did:example:labeler' |
| 12 | +process.env.SIGNING_KEY = '01'.repeat(32) |
| 13 | +process.env.TAP_URL = 'http://127.0.0.1:65535' |
| 14 | +process.env.HF_TOKEN = '' |
| 15 | + |
| 16 | +const { |
| 17 | + closeDb, |
| 18 | + getDb, |
| 19 | + logActivity, |
| 20 | + upsertOrganizationSnapshot, |
| 21 | +} = await import('../src/lib/db') |
| 22 | +const { fetchCurrentLabels, labelerServer } = await import('../src/labeler/server') |
| 23 | +const { recomputeLabeledOrganizationRow, syncLabelsWithDb } = await import('../src/labeler/tap-consumer') |
| 24 | + |
| 25 | +async function resetLabelDb(): Promise<void> { |
| 26 | + await (labelerServer as unknown as { dbInitLock?: Promise<void> }).dbInitLock |
| 27 | + await labelerServer.db.execute('DELETE FROM labels') |
| 28 | +} |
| 29 | + |
| 30 | +beforeEach(async () => { |
| 31 | + getDb().exec(` |
| 32 | + DELETE FROM activities; |
| 33 | + DELETE FROM organization_snapshots; |
| 34 | + DELETE FROM profile_snapshots; |
| 35 | + DELETE FROM pending_organization_deletes; |
| 36 | + DELETE FROM recompute_jobs; |
| 37 | + DELETE FROM actor_pds_cache; |
| 38 | + DELETE FROM url_checks; |
| 39 | + `) |
| 40 | + await resetLabelDb() |
| 41 | +}) |
| 42 | + |
| 43 | +after(() => { |
| 44 | + labelerServer.db.close() |
| 45 | + closeDb() |
| 46 | + rmSync(testDir, { recursive: true, force: true }) |
| 47 | +}) |
| 48 | + |
| 49 | +function organizationPayload(): OrganizationSnapshot['payload'] { |
| 50 | + return { |
| 51 | + $type: 'app.certified.actor.organization', |
| 52 | + organizationType: ['ngo'], |
| 53 | + createdAt: '2024-01-01T00:00:00.000Z', |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function storedLabelSubjects(): Promise<string[]> { |
| 58 | + const result = await labelerServer.db.execute('SELECT uri FROM labels ORDER BY id ASC') |
| 59 | + return result.rows.map(row => String(row.uri)) |
| 60 | +} |
| 61 | + |
| 62 | +test('recompute applies quality labels to the actor DID, not the organization record URI', async () => { |
| 63 | + const did = 'did:example:org-recompute' |
| 64 | + const rkey = 'self' |
| 65 | + const recordUri = `at://${did}/app.certified.actor.organization/${rkey}` |
| 66 | + |
| 67 | + upsertOrganizationSnapshot({ |
| 68 | + did, |
| 69 | + rkey, |
| 70 | + recordUri, |
| 71 | + payload: organizationPayload(), |
| 72 | + }) |
| 73 | + |
| 74 | + const outcome = await recomputeLabeledOrganizationRow(did) |
| 75 | + |
| 76 | + assert.ok(outcome) |
| 77 | + assert.equal(outcome.tier, 'standard') |
| 78 | + assert.deepEqual([...await fetchCurrentLabels(did)], ['standard']) |
| 79 | + assert.deepEqual([...await fetchCurrentLabels(recordUri)], []) |
| 80 | + assert.deepEqual(await storedLabelSubjects(), [did]) |
| 81 | +}) |
| 82 | + |
| 83 | +test('DB label sync repairs missing actor DID labels without labeling record URIs', async () => { |
| 84 | + const did = 'did:example:org-sync' |
| 85 | + const rkey = 'self' |
| 86 | + const recordUri = `at://${did}/app.certified.actor.organization/${rkey}` |
| 87 | + |
| 88 | + logActivity({ |
| 89 | + did, |
| 90 | + rkey, |
| 91 | + uri: recordUri, |
| 92 | + title: 'Sync Example Organization', |
| 93 | + score: 80, |
| 94 | + tier: 'high-quality', |
| 95 | + breakdown: '{}', |
| 96 | + testSignals: '[]', |
| 97 | + validationNotes: [], |
| 98 | + labeledAt: new Date().toISOString(), |
| 99 | + }) |
| 100 | + |
| 101 | + await syncLabelsWithDb() |
| 102 | + |
| 103 | + assert.deepEqual([...await fetchCurrentLabels(did)], ['high-quality']) |
| 104 | + assert.deepEqual([...await fetchCurrentLabels(recordUri)], []) |
| 105 | + assert.deepEqual(await storedLabelSubjects(), [did]) |
| 106 | +}) |
0 commit comments