Skip to content

Commit 196682a

Browse files
committed
fix(realtime): Enure realtimeExists() is called (#376)
1 parent 8cabd6a commit 196682a

File tree

4 files changed

+134
-5
lines changed

4 files changed

+134
-5
lines changed

packages/cli/src/commands/experimental/util.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export const printTaskEpilogue = (command, description, topicId) => {
4747
export const isServerFileSetup = () => {
4848
if (!serverFileExists()) {
4949
throw new Error(
50-
'RedwoodJS Realtime requires a serverful environment. Please run `yarn rw setup server-file` first.',
50+
'CedarJS Realtime requires a serverful environment. Please run `yarn ' +
51+
'cedarjs setup server-file` first.',
5152
)
5253
}
5354

@@ -63,9 +64,10 @@ export const realtimeExists = () => {
6364
}
6465

6566
export const isRealtimeSetup = () => {
66-
if (!realtimeExists) {
67+
if (!realtimeExists()) {
6768
throw new Error(
68-
'Adding realtime events requires that RedwoodJS Realtime be setup. Please run `yarn setup realtime` first.',
69+
'Adding realtime events requires that CedarJS Realtime is setup. ' +
70+
'Please run `yarn cedarjs setup realtime` first.',
6971
)
7072
}
7173

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import type FS from 'node:fs'
2+
3+
import { vi, describe, afterEach, beforeEach, it, expect } from 'vitest'
4+
5+
// @ts-expect-error - no types for JS files
6+
import { handler } from '../realtimeHandler.js'
7+
8+
const mocks = vi.hoisted(() => ({
9+
realtimeTs: '',
10+
serverTs: '',
11+
writtenFiles: {} as Record<string, string>,
12+
}))
13+
14+
vi.mock('fs-extra', async () => {
15+
const fs = await vi.importActual<typeof FS>('fs-extra')
16+
17+
return {
18+
default: {
19+
existsSync: (filePath: string) => {
20+
if (filePath === 'realtime.ts') {
21+
return !!mocks.realtimeTs
22+
}
23+
24+
if (filePath === 'server.ts') {
25+
return !!mocks.serverTs
26+
}
27+
28+
return !filePath.includes('foobar')
29+
},
30+
mkdirSync: () => {},
31+
readFileSync: (filePath: string) => {
32+
if (filePath.endsWith('blank.ts.template')) {
33+
return fs.readFileSync(filePath)
34+
}
35+
36+
return ''
37+
},
38+
writeFileSync: (dst: string, content: string) => {
39+
// Use `/` on both Windows and Unix
40+
mocks.writtenFiles[dst.replaceAll(/\\/g, '/')] = content
41+
},
42+
},
43+
}
44+
})
45+
46+
vi.mock('@cedarjs/internal/dist/generate/generate', () => ({
47+
generate: vi.fn().mockResolvedValue('success'),
48+
}))
49+
50+
vi.mock('@cedarjs/project-config', () => ({
51+
getPaths: () => {
52+
return {
53+
base: '',
54+
api: { src: '', lib: '', graphql: '', services: '', subscriptions: '' },
55+
web: { base: '', src: '' },
56+
}
57+
},
58+
getConfig: () => ({ experimental: { streamingSsr: { enabled: false } } }),
59+
resolveFile: (path: string) => path,
60+
}))
61+
62+
beforeEach(() => {
63+
vi.spyOn(console, 'log').mockImplementation(() => {})
64+
vi.spyOn(console, 'error').mockImplementation(() => {})
65+
vi.spyOn(process, 'exit').mockImplementation(() => void 0 as never)
66+
mocks.realtimeTs = 'export const realtime: RedwoodRealtimeOptions = {}'
67+
mocks.serverTs = 'export const serverFile: RedwoodServerFileOptions = {}'
68+
})
69+
70+
afterEach(() => {
71+
vi.mocked(console).log.mockRestore?.()
72+
vi.mocked(console).error.mockRestore?.()
73+
vi.mocked(process).exit.mockRestore?.()
74+
mocks.writtenFiles = {}
75+
})
76+
77+
describe('realtimeHandler', () => {
78+
it("Should print error and exit if server-file isn't setup", async () => {
79+
mocks.serverTs = ''
80+
81+
await handler({
82+
name: 'noRealtime',
83+
type: 'subscription',
84+
silent: true,
85+
})
86+
87+
expect(vi.mocked(console).error).toHaveBeenCalledWith(
88+
expect.stringMatching(
89+
'CedarJS Realtime requires a serverful environment. Please run `yarn ' +
90+
'cedarjs setup server-file` first.',
91+
),
92+
)
93+
expect(vi.mocked(process).exit).toHaveBeenCalledWith(1)
94+
})
95+
96+
it("Should print error and exit if Realtime isn't setup", async () => {
97+
mocks.realtimeTs = ''
98+
99+
await handler({
100+
name: 'noRealtime',
101+
type: 'subscription',
102+
silent: true,
103+
})
104+
105+
expect(vi.mocked(console).error).toHaveBeenCalledWith(
106+
expect.stringMatching(
107+
'Adding realtime events requires that CedarJS Realtime is setup. ' +
108+
'Please run `yarn cedarjs setup realtime` first.',
109+
),
110+
)
111+
expect(vi.mocked(process).exit).toHaveBeenCalledWith(1)
112+
})
113+
114+
it('should generate a pubSub subscription', async () => {
115+
await handler({
116+
name: 'foobar',
117+
type: 'subscription',
118+
silent: true,
119+
})
120+
121+
expect(mocks.writtenFiles['foobar/foobar.ts']).toMatch(
122+
"pubSub.subscribe('Foobar', id)",
123+
)
124+
})
125+
})

packages/cli/src/commands/generate/realtime/realtimeHandler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const templateVariables = (name) => {
4141
}
4242
}
4343

44-
export async function handler({ name, type, force, verbose }) {
44+
export async function handler({ name, type, force, verbose, silent }) {
4545
const redwoodPaths = getPaths()
4646
const ts = isTypeScriptProject()
4747
name = singular(name.toLowerCase())
@@ -242,7 +242,7 @@ export async function handler({ name, type, force, verbose }) {
242242
],
243243
{
244244
rendererOptions: { collapseSubtasks: false, persistentOutput: true },
245-
renderer: verbose ? 'verbose' : 'default',
245+
renderer: silent ? 'silent' : verbose ? 'verbose' : 'default',
246246
},
247247
)
248248

packages/testing/build.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ fs.rmSync('./config', { recursive: true, force: true })
3434
fs.mkdirSync('./config')
3535
fs.cpSync('./dist/cjs/config', './config', { recursive: true })
3636
fs.cpSync('./dist/cjs/package.json', './config/package.json')
37+
fs.cpSync('./dist/cjs/package.json', './config/jest/api/package.json')
38+
fs.cpSync('./dist/cjs/package.json', './config/jest/web/package.json')
3739

3840
// Replace relative imports with absolute @cedarjs/testing/dist/cjs imports in built CJS files
3941
function replaceImportsInFile(filePath: string) {

0 commit comments

Comments
 (0)