Skip to content

Commit fdd0d42

Browse files
committed
Tests: Use shared server instance to support testing plugin zip archive before publish; Run tests for framework and template system
1 parent 5d57e88 commit fdd0d42

2 files changed

Lines changed: 53 additions & 23 deletions

File tree

tests/now/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,26 @@ export default run(async () => {
3131
test('Plugin', async () => {
3232

3333
result = await wpx`return test\\get_active_plugins();`
34-
console.log('Active plugins', result)
34+
// console.log('Active plugins', result)
3535
is(true, result.includes('now/plugin.php'), 'test plugin is active')
3636

3737
result = await wpx`return test\\get_all_plugins();`
38-
console.log('All plugins', result)
38+
// console.log('All plugins', result)
3939

4040
result = await wpx`return test\\activate_dependency_plugins();`
4141
is(true, result, 'activate dependency plugins')
4242
})
43+
44+
await import(`../../vendor/tangible/framework/tests/index.ts`)
45+
46+
for (const key of [
47+
'loop',
48+
'logic',
49+
'language',
50+
'admin',
51+
]) {
52+
await import(`../../vendor/tangible/template-system/tests/${key}/index.ts`)
53+
}
4354
})
4455

4556
type PluginInfos = {

tests/now/server.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ export function createRequest(siteUrl: string): Requester {
6464
}
6565
}
6666

67-
return await (await fetch(url, options))[
68-
format
69-
]()
67+
return await (await fetch(url, options))[format]()
7068
}
7169

7270
request.token = undefined
@@ -81,6 +79,8 @@ const silentConsole = {
8179
log() {},
8280
warn() {},
8381
error() {},
82+
info() {},
83+
assert() {},
8484
}
8585
export const originalConsole = globalThis.console
8686

@@ -93,8 +93,7 @@ export const enableConsole = () => {
9393
globalThis.console = originalConsole
9494
}
9595

96-
97-
let serverInstance: Server | null
96+
// let serverInstance: Server | null
9897

9998
export type Server = {
10099
php: PHP
@@ -114,6 +113,8 @@ export type Server = {
114113
resetSiteTemplate: () => void
115114

116115
onMessage: (callback: Listener) => void
116+
117+
template: (code: string | TemplateStringsArray, ...args: string[]) => any
117118
}
118119

119120
export type Listener = (message: any) => void
@@ -131,9 +132,9 @@ export async function getServer(
131132
reset?: boolean
132133
} = {},
133134
): Promise<Server> {
134-
if (serverInstance) {
135-
if (!options.restart) return serverInstance
136-
await serverInstance.stopServer()
135+
if (globalThis.serverInstance) {
136+
if (!options.restart) return globalThis.serverInstance
137+
await globalThis.serverInstance.stopServer()
137138
}
138139

139140
const {
@@ -150,7 +151,7 @@ export async function getServer(
150151
path: projectPath,
151152
blueprint,
152153
env,
153-
// @ts-ignore
154+
// @ts-ignore
154155
mappings,
155156
})),
156157

@@ -163,10 +164,7 @@ export async function getServer(
163164
} as WPNowOptions)
164165

165166
const { php, stopServer } = server
166-
const {
167-
port = 3000,
168-
documentRoot = '/var/www/html'
169-
} = server.options
167+
const { port = 3000, documentRoot = '/var/www/html' } = server.options
170168

171169
/**
172170
* PHP-WASM provides a function post_message_to_js() to send messages from PHP to JS.
@@ -192,8 +190,8 @@ export async function getServer(
192190
const message = JSON.parse(data)
193191
for (const listener of phpListeners) {
194192
listener(message)
195-
}
196-
} catch(e) {
193+
}
194+
} catch (e) {
197195
console.error(e)
198196
}
199197
})
@@ -210,7 +208,10 @@ export async function getServer(
210208
*
211209
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates
212210
*/
213-
const phpx = async (code: string | TemplateStringsArray, ...args: string[]) => {
211+
const phpx = async (
212+
code: string | TemplateStringsArray,
213+
...args: string[]
214+
) => {
214215
if (Array.isArray(code)) {
215216
code = code.reduce(
216217
(prev, now, index) => prev + now + (args[index] ?? ''),
@@ -243,7 +244,10 @@ export async function getServer(
243244
* const result = wpx`return 'hi';`
244245
* ```
245246
*/
246-
const wpx = async (code: string | TemplateStringsArray, ...args: string[]) => {
247+
const wpx = async (
248+
code: string | TemplateStringsArray,
249+
...args: string[]
250+
) => {
247251
if (Array.isArray(code)) {
248252
code = code.reduce(
249253
(prev, now, index) => prev + now + (args[index] ?? ''),
@@ -257,13 +261,13 @@ export async function getServer(
257261

258262
for (const line of (code as string).split('\n')) {
259263
if (line.trim().startsWith('use ')) {
260-
useNamespace += line + '\n';
264+
useNamespace += line + '\n'
261265
} else {
262266
filteredCode += line + '\n'
263267
}
264268
}
265269

266-
const result = await phpx/* php */`
270+
const result = await phpx/* php */ `
267271
${useNamespace}
268272
include 'wp-load.php';
269273
echo json_encode((function() {
@@ -321,7 +325,7 @@ HTML);
321325

322326
const siteUrl = `http://localhost:${port}`
323327

324-
return (serverInstance = {
328+
return (globalThis.serverInstance = {
325329
php,
326330
port,
327331
console: originalConsole,
@@ -332,11 +336,26 @@ HTML);
332336
phpx,
333337
wpx,
334338
async stopServer() {
335-
serverInstance = null
339+
globalThis.serverInstance = null
336340
await stopServer()
337341
},
338342
onMessage,
339343
setSiteTemplate,
340344
resetSiteTemplate,
345+
346+
async template(code: string | TemplateStringsArray, ...args: string[]) {
347+
if (Array.isArray(code)) {
348+
code = code.reduce(
349+
(prev, now, index) => prev + now + (args[index] ?? ''),
350+
'',
351+
)
352+
}
353+
return (
354+
await wpx/* php */ `
355+
return tangible_template(<<<'HTML'
356+
${code}
357+
HTML);`
358+
).trim()
359+
},
341360
})
342361
}

0 commit comments

Comments
 (0)