Skip to content

Commit 69ead40

Browse files
committed
more tests
1 parent 9baa679 commit 69ead40

File tree

12 files changed

+125
-88
lines changed

12 files changed

+125
-88
lines changed

src/core/scad-bin.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { homedir, platform } from "node:os";
22
import { join } from "node:path";
3+
import debug from "../lib/debug";
4+
import type { PlatformMap } from "../types";
35

46
/**
57
* Default OpenSCAD install locations
@@ -43,11 +45,16 @@ export const SCAD_BINS = {
4345
/**
4446
* Helper: Returns the OpenSCAD binary path for the current platform.
4547
*/
46-
export function autoBinPath(which: null | "auto" | "nightly" = "auto") {
48+
export function autoBinPath(binType: null | "auto" | "nightly" = "auto") {
49+
const log = debug.extend("bin");
4750
const p = platform();
48-
const map = which === "nightly" ? SCAD_BIN_NIGHTLY : SCAD_BIN;
49-
const bin = map[p as keyof PlatformMap];
50-
return typeof bin === "string" ? bin : null;
51+
const binMap = binType === "nightly" ? SCAD_BIN_NIGHTLY : SCAD_BIN;
52+
const bin = binMap[p as keyof PlatformMap];
53+
const retVal = typeof bin === "string" ? bin : null;
54+
log("platform: %s", p);
55+
log("binType: %s", binType);
56+
log("output: %s", retVal);
57+
return retVal;
5158
}
5259

5360
/**
@@ -60,9 +67,3 @@ export function macosUserInstalledOpenSCAD() {
6067
}
6168
return join(homedir(), ...SCAD_BIN.darwin.split("/"));
6269
}
63-
64-
type PlatformMap = {
65-
darwin: string;
66-
linux: string;
67-
win32: string;
68-
};

src/core/themes.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ export const THEMES = [
99
"Ultramarine",
1010
] as const;
1111

12-
export const theme = (t: PluginTheme) => t;
13-
14-
export type PluginTheme = (typeof THEMES)[number];
12+
export const theme = (t: (typeof THEMES)[number]) => t;

src/lib/debug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import Debug from "debug";
22
export const debug = Debug("eleventy:scad");
3+
export default debug;

src/types.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import type { EleventyScope, EleventySuppliedData } from "11ty.ts";
22
import type z from "zod";
3-
import type { PluginOptionsSchema } from "./core";
3+
import type { PluginOptionsSchema, THEMES } from "./core";
44

55
export type * from "11ty.ts";
66

7+
export type ModelViewerTheme = (typeof THEMES)[number];
8+
9+
export type PluginOptions = z.infer<typeof PluginOptionsSchema>;
10+
11+
export type ParsedPluginOptions = z.output<typeof PluginOptionsSchema>;
12+
713
export type PluginOptionsInput = Omit<
814
z.input<typeof PluginOptionsSchema>,
915
"launchPath"
1016
> & {
1117
launchPath: "auto" | "nightly" | string;
1218
};
1319

14-
export type ParsedPluginOptions = z.output<typeof PluginOptionsSchema>;
15-
16-
export type PluginOptions = z.infer<typeof PluginOptionsSchema>;
17-
1820
export type ScadTemplateData = {
1921
layout: string;
2022
title: string;
@@ -42,3 +44,10 @@ export type EleventyDirs = {
4244
layouts?: string;
4345
output: string;
4446
};
47+
48+
export type MainPlatforms = Extract<
49+
"linux" | "darwin" | "win32",
50+
NodeJS.Platform
51+
>;
52+
53+
export type PlatformMap = Record<MainPlatforms, string>;

test/JsonMode.test.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { beforeAll, beforeEach, describe, expect, it } from "vitest";
2-
import { createTestInstance } from "./_setup/11ty-scad";
2+
import { createTestInstance, TEST_SCAD_PAGES } from "./_setup/eleventy";
33

4-
const escad = createTestInstance({
4+
const eleventy = createTestInstance({
55
launchPath: "nightly",
66
});
77

88
const pages: EleventyPageJSON[] = [];
99

10-
describe.for([["cube"], ["sphere"], ["cylinder"]])("%s.scad", ([name]) => {
11-
let page: EleventyPageJSON | undefined;
10+
beforeAll(async () => {
11+
const generated = await eleventy.toJSON();
12+
pages.push(...generated);
13+
});
1214

13-
beforeAll(async () => {
14-
const generated = await escad.toJSON();
15-
pages.push(...generated);
16-
});
15+
describe.for(TEST_SCAD_PAGES)("%s.scad", ([name]) => {
16+
let page: EleventyPageJSON | undefined;
1717

1818
beforeEach(() => {
1919
page = pages.find((p) => p.url.includes(name));
@@ -38,31 +38,27 @@ describe.for([["cube"], ["sphere"], ["cylinder"]])("%s.scad", ([name]) => {
3838
});
3939

4040
describe("(virtual) index.html", () => {
41-
let page: EleventyPageJSON;
41+
let page: EleventyPageJSON | undefined;
4242

4343
beforeAll(() => {
44-
const _page = pages.find((p) => p.url === "/");
45-
if (!_page) {
46-
throw new Error("This will never throw unless the page didn't generate");
47-
}
48-
page = _page;
44+
page = pages.find((p) => p.url === "/");
4945
});
5046

5147
it("has the correct URL", () => {
52-
expect(page.url).toBe("/");
48+
expect(page?.url).toBe("/");
5349
});
5450

5551
it("has valid HTML content", () => {
5652
expect(page?.content).toStartWithString("<!DOCTYPE html>");
5753
});
5854

5955
it("has the correct input path", () => {
60-
expect(page.inputPath).toEndWithString(`input/index.njk`);
56+
expect(page?.inputPath).toEndWithString(`input/index.njk`);
6157
});
6258

6359
it("generated the correct output HTML file", () => {
64-
expect(page.outputPath).toEndWithString(`output/index.html`);
60+
expect(page?.outputPath).toEndWithString(`output/index.html`);
6561
});
6662
});
6763

68-
type EleventyPageJSON = Awaited<ReturnType<typeof escad.toJSON>>[number];
64+
type EleventyPageJSON = Awaited<ReturnType<typeof eleventy.toJSON>>[number];

test/Plugin.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, test } from "vitest";
2-
import { createTestInstance } from "./_setup/11ty-scad";
2+
import { createTestInstance } from "./_setup/eleventy";
33

44
describe("Eleventy OpenSCAD Plugin", () => {
55
test.only("bad launchPath throws an Error", async () => {

test/Themes.test.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
import { describe, expect, it } from "vitest";
2-
import { SCAD_EXT } from "../src/core";
3-
import { createTestInstance, DISABLE_OPENSCAD } from "./_setup/11ty-scad";
4-
import type { StlViewerThemes } from "../src";
2+
import { SCAD_EXT, THEMES } from "../src/core";
3+
import { createTestInstance, DISABLE_OPENSCAD } from "./_setup/eleventy";
54

6-
const THEMES: StlViewerThemes[][] = [
7-
["Chocolate"],
8-
["Midnight"],
9-
["Modernist"],
10-
["Oldstyle"],
11-
["Steely"],
12-
["Swiss"],
13-
["Traditional"],
14-
["Ultramarine"],
15-
];
5+
const cases = THEMES.map((t) => [t]);
166

17-
describe.for(THEMES)("Theme: %s", ([theme]) => {
18-
const escad = createTestInstance({
7+
describe.for(cases)("%s", ([theme]) => {
8+
const themeURL = `https://www.w3.org/StyleSheets/Core/${theme}`;
9+
10+
const eleventy = createTestInstance({
1911
launchPath: "nightly",
2012
theme,
2113
silent: true,
2214
...DISABLE_OPENSCAD,
2315
});
2416

25-
it("has the correct theme css", async () => {
26-
const pages = await escad.toJSON();
17+
it("has the CSS URL in page", async () => {
18+
const pages = await eleventy.toJSON();
2719
const scadPages = pages.filter((p) => p.inputPath.endsWith(SCAD_EXT));
2820

2921
expect(scadPages).toHaveLength(3);
3022
for (const page of scadPages) {
31-
expect(page.content.includes(theme)).toBeTruthy();
23+
expect(page.content.includes(themeURL)).toBeTruthy();
3224
}
3325
});
3426
});

test/WriteMode.test.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
import path from "node:path";
2+
import { rimraf } from "rimraf";
23
import { beforeAll, describe, expect, it } from "vitest";
3-
import { createTestInstance, TEST_SITE_OUTPUT } from "./_setup/11ty-scad";
4+
import {
5+
createTestInstance,
6+
TEST_SCAD_PAGES,
7+
TEST_SITE_OUTPUT,
8+
} from "./_setup/eleventy";
49

5-
const escad = createTestInstance({
10+
const eleventy = createTestInstance({
611
launchPath: "nightly",
712
});
813

9-
describe("WRITE Mode", () => {
10-
beforeAll(async () => {
11-
// await rimraf(path.join(ELEVENTY_TEST_OUTPUT, "*"), { glob: true });
12-
await escad.write();
14+
beforeAll(async () => {
15+
await rimraf(path.join(TEST_SITE_OUTPUT, "*"), { glob: true });
16+
await eleventy.write();
17+
});
18+
19+
describe("(virtual) index.html", () => {
20+
it(`generated "index.html"`, () => {
21+
const index = path.join(TEST_SITE_OUTPUT, "index.html");
22+
expect(index).toExist();
1323
});
24+
});
1425

15-
describe("cube.scad", () => {
16-
const generatedDir = path.join(TEST_SITE_OUTPUT, "cube");
26+
describe.for(TEST_SCAD_PAGES)("%s.scad", ([name]) => {
27+
const generatedDir = path.join(TEST_SITE_OUTPUT, name);
1728

18-
it(`generated "cube/index.html"`, () => {
19-
const index = path.join(generatedDir, "index.html");
20-
expect(index).toExist();
21-
});
29+
it(`generated "${name}/index.html"`, () => {
30+
const index = path.join(generatedDir, "index.html");
31+
expect(index).toExist();
32+
});
2233

23-
it(`generated "cube/cube.stl"`, () => {
24-
const stl = path.join(generatedDir, "cube.stl");
25-
expect(stl).toExist();
26-
});
34+
it(`generated "${name}/${name}.stl"`, () => {
35+
const stl = path.join(generatedDir, `${name}.stl`);
36+
expect(stl).toExist();
2737
});
2838
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const DISABLE_OPENSCAD = {
1414
checkLaunchPath: false,
1515
};
1616

17-
export const TEST_SACD_PAGES = [["cube"], ["sphere"], ["cylinder"]] as const;
17+
export const TEST_SCAD_PAGES = [["cube"], ["sphere"], ["cylinder"]] as const;
1818

1919
export function createTestInstance(options: PluginOptionsInput): Eleventy {
2020
return new Eleventy(TEST_SITE_INPUT, TEST_SITE_OUTPUT, {

test/core/options.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
import { describe, expect, it } from "vitest";
1+
import { platform } from "node:process";
2+
import { describe, expect, it, test } from "vitest";
3+
import { ZodError } from "zod";
4+
import { autoBinPath, SCAD_BINS } from "../../src/core";
25
import { PluginOptionsSchema } from "../../src/core/options";
36

47
describe("PluginOptionsSchema", () => {
58
it("has sane defaults with no options set", () => {
69
const o = PluginOptionsSchema.parse({});
7-
810
expect(o).toMatchObject({
11+
theme: "Midnight",
912
checkLaunchPath: true,
1013
collectionPage: true,
1114
noSTL: false,
1215
silent: false,
13-
theme: "Midnight",
1416
verbose: false,
1517
});
1618
});
19+
20+
it("errors with an invalid theme", () => {
21+
const o = PluginOptionsSchema.safeParse({
22+
theme: "TacoBell#324",
23+
});
24+
expect(o.success).toBeFalsy();
25+
expect(o.error).toBeInstanceOf(ZodError);
26+
});
1727
});

0 commit comments

Comments
 (0)