Skip to content

Commit

Permalink
test: add image URL "homebrew-img" source check
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGiddyLimit committed Apr 16, 2024
1 parent 574a837 commit cb8b03b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ There are a few conventions used which should be followed when creating homebrew
- Use `https://github.com/TheGiddyLimit/homebrew` as the source URL for sources without one.
- Only include content authors in the source `"author"` field; conversion credit should be given in a `"convertedBy"` field (with the same format).
- Include a `"dateAdded"` property in file metadata, which is a Unix timestamp (in seconds) at which the file was added. See [here](https://github.com/TheGiddyLimit/homebrew/blob/master/spell/Sample%20-%20Giddy%3B%20Assorted%20Marginalia.json#L29) for an example of the structure; an example timestamp would be `1537874753`. You can view and copy the current Unix time [here](https://www.epochconverter.com/).
- Images (and similar assets, e.g. character sheet PDFs) should be added to the [homebrew image repository](https://github.com/TheGiddyLimit/homebrew-img)

#### Useful links

Expand Down
4 changes: 3 additions & 1 deletion _test/test-tags.js → _test/test-file-contents.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";
import {DataTester, BraceCheck, EscapeCharacterCheck} from "5etools-utils";
import * as Uf from "5etools-utils/lib/UtilFs.js";
import fs from "fs";
import {ImageUrlCheck} from "./test-file-contents/ImageUrlCheck.js";

const TIME_TAG = "\tRun duration";
console.time(TIME_TAG);
Expand All @@ -9,6 +10,7 @@ async function main () {
const ClazzDataTesters = [
BraceCheck,
EscapeCharacterCheck,
ImageUrlCheck,
];
DataTester.register({ClazzDataTesters});

Expand Down
53 changes: 53 additions & 0 deletions _test/test-file-contents/ImageUrlCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {DataTesterBase} from "5etools-utils/lib/TestData.js";
import {ObjectWalker} from "5etools-utils/lib/ObjectWalker.js";

export class ImageUrlCheck extends DataTesterBase {
static _URL_PREFIX_HOMEBREW_IMG = `https://raw.githubusercontent.com/TheGiddyLimit/homebrew-img/main/`;
static _RE_HOMEBREW_IMG_PATH = /^(?<type>img|pdf)\/(?<source>[^/]+)\//;

static registerParsedFileCheckers (parsedJsonChecker) {
parsedJsonChecker.registerFileHandler(this);
}

static handleFile (file, contents) {
const checker = new this({contents});

checker._walk({file, contents});
}

constructor ({contents}) {
super();
this._sources = new Set(
(contents._meta?.sources?.map(src => src?.json) || [])
.filter(Boolean),
);
}

_walk ({file, contents}) {
ObjectWalker.walk({
obj: contents,
filePath: file,
primitiveHandlers: {
object: this._checkObject.bind(this),
},
});
}

_checkObject (obj, {filePath}) {
if (obj.type !== "image" || obj.href?.type !== "external" || !obj.href?.url) return;

const {url} = obj.href;
if (!url.toLowerCase().startsWith(this.constructor._URL_PREFIX_HOMEBREW_IMG.toLowerCase())) return;

const mPath = this.constructor._RE_HOMEBREW_IMG_PATH.exec(url.slice(this.constructor._URL_PREFIX_HOMEBREW_IMG.length));
if (!mPath) {
this.constructor._addMessage(`Unknown "homebrew-img" URL pattern in file "${filePath}": "${url}"\n`);
return;
}

const {source, type} = mPath.groups;
if (this._sources.has(source)) return;

this.constructor._addMessage(`Image source part "${source}" in "homebrew-img" ${type} URL did not match sources found in file "_meta" in file "${filePath}": "${url}"\n`);
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"build:clean:base": "node _node/clean-and-test.js",
"build:clean:html": "node _node/clean-html.js",
"test:json": "test-json-brew",
"test:tags": "node _test/test-tags.js",
"test:file-contents": "node _test/test-file-contents.js",
"test:file-locations": "test-file-locations",
"test:file-extensions": "test-file-extensions",
"test": "npm run build:clean && npm run build:index && npm run test:json && npm run test:file-locations && npm run test:file-extensions && npm run test:tags"
"test": "npm run build:clean && npm run build:index && npm run test:json && npm run test:file-locations && npm run test:file-extensions && npm run test:file-contents"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit cb8b03b

Please sign in to comment.