Skip to content

Commit

Permalink
fix: unit tests compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Jan 27, 2024
1 parent 6a64c71 commit 6277f1b
Show file tree
Hide file tree
Showing 9 changed files with 1,603 additions and 568 deletions.
2,128 changes: 1,574 additions & 554 deletions packages/compiler/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"vite-node": "^0.30.1",
"vite-plugin-pwa": "^0.16.4",
"vite-plugin-require-transform": "^1.0.12",
"vitest": "^0.30.1",
"vitest": "^1.2.2",
"vitest-webgl-canvas-mock": "^1.1.0",
"xml2js": "^0.5.0",
"yargs": "^17.7.1"
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler/src/build/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ export interface ClientBuildConfigOptions {
optimizeDepsExclude?: string[]
}

export async function clientBuildConfig(dirname: string, options: ClientBuildConfigOptions = {}, config: any) {
export async function clientBuildConfig(dirname: string, options: ClientBuildConfigOptions = {}, config?) {
const isServer = options.side === 'server'
const isTest = options.mode === 'test'
const isRpg = options.type === 'rpg'
const isBuild = options.serveMode === false
const mode = options.mode || 'development'
const plugin = options.plugin
const { cwd, env } = process
config = config ?? await loadConfigFile(mode)

const envType = env.RPG_TYPE
if (envType && !['rpg', 'mmorpg'].includes(envType)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/build/load-global-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function loadGlobalConfig(modules: string[], config: Config, options: Cli
}
const configPath = path.resolve(process.cwd(), modulePath, 'config.json')
if (fs.existsSync(configPath)) {
const configFile: any = fs.readFileSync(configPath, 'utf-8')
const configFile: any = fs.readFileSync(configPath).toString()
const jsonFile = JSON.parse(configFile)
if (jsonFile.namespace) namespaces.push(jsonFile.namespace)
parseSchema(jsonFile, module)
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/build/vite-plugin-config.toml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export function createModuleLoad(id: string, variableName: string, modulePath: s
const indexFile = path.join(modulePathId, 'index.ts')

if (fs.existsSync(packageJson)) {
const { main: entryPoint } = JSON.parse(fs.readFileSync(packageJson, 'utf-8'))
const { main: entryPoint } = JSON.parse(fs.readFileSync(packageJson).toString())
if (entryPoint) {
const mod = toPosix(path.join(id, entryPoint))
return dd`
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/src/build/vite-plugin-map-extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { globFiles } from './utils.js';
async function processTsxFile(tsxFile: string, output: string) {
if (tsxFile.includes('gui')) return

const content = fs.readFileSync(tsxFile, 'utf-8');
const content = fs.readFileSync(tsxFile).toString()
const result = await parseStringPromise(content);
const imagePath = path.join(path.dirname(tsxFile), result.tileset.image[0].$.source);

Expand All @@ -16,7 +16,7 @@ async function processTsxFile(tsxFile: string, output: string) {

// Process a TMX file and copy all its images to the output directory
async function processTmxFile(tmxFile: string, output: string) {
const content = fs.readFileSync(tmxFile, 'utf-8');
const content = fs.readFileSync(tmxFile).toString()
const result = await parseStringPromise(content);

// Copy an image from a given source path to the output directory
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler/tests/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Test Structure files After Build', () => {
expect(clientFiles).toEqual(['assets', 'index.html', 'manifest.json', 'manifest.webmanifest', 'registerSW.js', 'sw.js', 'workbox-fa446783.js'])

const serverFiles = fs.readdirSync('dist/server')
expect(serverFiles).toEqual(['assets', 'main.mjs', 'manifest.json'])
expect(serverFiles).toEqual(['assets', 'main.js', 'manifest.json'])
})

test('Move TMX files', async () => {
Expand Down Expand Up @@ -128,6 +128,7 @@ describe('Test Structure files After Build', () => {
})

test('Move TMX files', async () => {

mockFs({
...defaultFiles,
'main/maps': mapStructure,
Expand Down
9 changes: 2 additions & 7 deletions packages/compiler/tests/toml.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Vi, { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from
import * as path from 'path'
import mockFs from 'mock-fs'
import sizeOf from 'image-size'
import { ClientBuildConfigOptions, Config } from '../src/build/client-config'
import { ClientBuildConfigOptions } from '../src/build/client-config'
import { Config, loadConfigFile } from '../src/build/load-config-file'

vi.mock('image-size')

Expand Down Expand Up @@ -305,12 +306,6 @@ describe('TOML Configuration test', () => {
config = {};
});

test("should correctly set startMap from config.start.map if it exists", () => {
config.start = { map: 'testMap' };
configTomlPlugin(options, config);
expect(config.startMap).toEqual('testMap');
});

test("should return Plugin object when global config loads successfully", () => {
// Simulate successful loading of global config
const result = configTomlPlugin(options, config);
Expand Down
18 changes: 18 additions & 0 deletions packages/compiler/tests/vite-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ describe('clientBuildConfig', () => {
expect(ret.build.outDir).toContain('dist/mydir');
})
})

describe('Start Map', () => {
test("should correctly set startMap from config.start.map if it exists", async () => {
mockFs({
'rpg.toml': `
[start]
map = 'testMap'
`,
'index.html': '',
'package.json': '{"name": "test"}'
});
const ret = await clientBuildConfig('.', {
type: 'mmorpg',
serveMode: false,
});
expect(ret._projectConfig.startMap).toBe('testMap');
});
})
})

describe('Test RPG Mode', () => {
Expand Down

0 comments on commit 6277f1b

Please sign in to comment.