@eduardbar/drift 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ import { afterEach, describe, expect, it } from 'vitest'
2
+ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
3
+ import { tmpdir } from 'node:os'
4
+ import { join } from 'node:path'
5
+ import { analyzeProject } from '../src/analyzer.js'
6
+ import { buildReport } from '../src/reporter.js'
7
+ import { ingestSnapshotFromReport, getSaasSummary } from '../src/saas.js'
8
+
9
+ function createProjectDir(prefix: string): string {
10
+ const dir = mkdtempSync(join(tmpdir(), prefix))
11
+ writeFileSync(join(dir, 'index.ts'), 'export const value = 1\n')
12
+ return dir
13
+ }
14
+
15
+ function createReport(projectDir: string) {
16
+ const files = analyzeProject(projectDir)
17
+ return buildReport(projectDir, files)
18
+ }
19
+
20
+ describe('saas foundations', () => {
21
+ const dirs: string[] = []
22
+
23
+ afterEach(() => {
24
+ for (const dir of dirs.splice(0)) {
25
+ rmSync(dir, { recursive: true, force: true })
26
+ }
27
+ })
28
+
29
+ it('ingest creates snapshot and summary stays consistent', () => {
30
+ const projectDir = createProjectDir('drift-saas-ingest-')
31
+ dirs.push(projectDir)
32
+ const storeFile = join(projectDir, '.drift-cloud', 'store.json')
33
+
34
+ const report = createReport(projectDir)
35
+ const snapshot = ingestSnapshotFromReport(report, {
36
+ workspaceId: 'ws-1',
37
+ userId: 'user-1',
38
+ repoName: 'repo-1',
39
+ storeFile,
40
+ })
41
+
42
+ const summary = getSaasSummary({ storeFile })
43
+
44
+ expect(snapshot.workspaceId).toBe('ws-1')
45
+ expect(summary.usersRegistered).toBe(1)
46
+ expect(summary.workspacesActive).toBe(1)
47
+ expect(summary.reposActive).toBe(1)
48
+ expect(summary.totalSnapshots).toBe(1)
49
+ expect(summary.phase).toBe('free')
50
+ })
51
+
52
+ it('blocks ingest when workspace exceeds repo limit', () => {
53
+ const projectDir = createProjectDir('drift-saas-repo-limit-')
54
+ dirs.push(projectDir)
55
+ const storeFile = join(projectDir, '.drift-cloud', 'store.json')
56
+ const report = createReport(projectDir)
57
+
58
+ ingestSnapshotFromReport(report, {
59
+ workspaceId: 'ws-2',
60
+ userId: 'user-1',
61
+ repoName: 'repo-a',
62
+ storeFile,
63
+ policy: { maxReposPerWorkspace: 1 },
64
+ })
65
+
66
+ expect(() => {
67
+ ingestSnapshotFromReport(report, {
68
+ workspaceId: 'ws-2',
69
+ userId: 'user-1',
70
+ repoName: 'repo-b',
71
+ storeFile,
72
+ policy: { maxReposPerWorkspace: 1 },
73
+ })
74
+ }).toThrow(/max repos/i)
75
+
76
+ const summary = getSaasSummary({ storeFile, policy: { maxReposPerWorkspace: 1 } })
77
+ expect(summary.totalSnapshots).toBe(1)
78
+ })
79
+
80
+ it('summary reflects free to paid threshold transition', () => {
81
+ const projectDir = createProjectDir('drift-saas-threshold-')
82
+ dirs.push(projectDir)
83
+ const storeFile = join(projectDir, '.drift-cloud', 'store.json')
84
+ const report = createReport(projectDir)
85
+
86
+ ingestSnapshotFromReport(report, {
87
+ workspaceId: 'ws-3',
88
+ userId: 'u-1',
89
+ repoName: 'repo-a',
90
+ storeFile,
91
+ policy: { freeUserThreshold: 2 },
92
+ })
93
+ ingestSnapshotFromReport(report, {
94
+ workspaceId: 'ws-4',
95
+ userId: 'u-2',
96
+ repoName: 'repo-b',
97
+ storeFile,
98
+ policy: { freeUserThreshold: 2 },
99
+ })
100
+
101
+ const summary = getSaasSummary({ storeFile, policy: { freeUserThreshold: 2 } })
102
+ expect(summary.usersRegistered).toBe(2)
103
+ expect(summary.thresholdReached).toBe(true)
104
+ expect(summary.phase).toBe('paid')
105
+ expect(summary.freeUsersRemaining).toBe(0)
106
+ })
107
+ })