| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { generateHash, HootError, isOfType, normalize } from "../hoot_utils"; |
| 4 |
import { applyTags } from "./tag"; |
| 5 |
|
| 6 |
/** |
| 7 |
* @typedef {{ |
| 8 |
* debug?: boolean; |
| 9 |
* multi?: number; |
| 10 |
* only?: boolean; |
| 11 |
* skip?: boolean; |
| 12 |
* timeout?: number; |
| 13 |
* todo?: boolean; |
| 14 |
* }} JobConfig |
| 15 |
* |
| 16 |
* @typedef {import("./tag").Tag} Tag |
| 17 |
*/ |
| 18 |
|
| 19 |
//----------------------------------------------------------------------------- |
| 20 |
// Global |
| 21 |
//----------------------------------------------------------------------------- |
| 22 |
|
| 23 |
const { |
| 24 |
Object: { assign: $assign, entries: $entries }, |
| 25 |
Symbol, |
| 26 |
} = globalThis; |
| 27 |
|
| 28 |
//----------------------------------------------------------------------------- |
| 29 |
// Internal |
| 30 |
//----------------------------------------------------------------------------- |
| 31 |
|
| 32 |
/** |
| 33 |
* @param {JobConfig} config |
| 34 |
*/ |
| 35 |
function validateConfig(config) { |
| 36 |
for (const [key, value] of $entries(config)) { |
| 37 |
if (!isOfType(value, CONFIG_TAG_SCHEMA[key])) { |
| 38 |
throw new HootError(`invalid config tag: parameter "${key}" does not exist`, { |
| 39 |
level: "critical", |
| 40 |
}); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** @type {Record<keyof JobConfig, import("../hoot_utils").ArgumentType>} */ |
| 46 |
const CONFIG_TAG_SCHEMA = { |
| 47 |
debug: "boolean", |
| 48 |
multi: "number", |
| 49 |
only: "boolean", |
| 50 |
skip: "boolean", |
| 51 |
timeout: "number", |
| 52 |
todo: "boolean", |
| 53 |
}; |
| 54 |
|
| 55 |
const S_MINIMIZED = Symbol("minimized"); |
| 56 |
|
| 57 |
//----------------------------------------------------------------------------- |
| 58 |
// Exports |
| 59 |
//----------------------------------------------------------------------------- |
| 60 |
|
| 61 |
export class Job { |
| 62 |
/** @type {JobConfig} */ |
| 63 |
config = {}; |
| 64 |
/** @type {Job[]} */ |
| 65 |
path = [this]; |
| 66 |
runCount = 0; |
| 67 |
/** @type {Tag[]} */ |
| 68 |
tags = []; |
| 69 |
|
| 70 |
get isMinimized() { |
| 71 |
return S_MINIMIZED in this; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param {import("./suite").Suite | null} parent |
| 76 |
* @param {string} name |
| 77 |
* @param {JobConfig & { tags?: Iterable<Tag> }} config |
| 78 |
*/ |
| 79 |
constructor(parent, name, config) { |
| 80 |
this.parent = parent || null; |
| 81 |
this.name = name; |
| 82 |
|
| 83 |
if (this.parent) { |
| 84 |
// Assigns parent path and config (ignoring multi) |
| 85 |
const parentConfig = { |
| 86 |
...this.parent.config, |
| 87 |
tags: this.parent.tags, |
| 88 |
}; |
| 89 |
delete parentConfig.multi; |
| 90 |
this.configure(parentConfig); |
| 91 |
this.path.unshift(...this.parent.path); |
| 92 |
} |
| 93 |
|
| 94 |
this.fullName = this.path.map((job) => job.name).join("/"); |
| 95 |
this.id = generateHash(this.fullName); |
| 96 |
this.key = normalize(this.fullName); |
| 97 |
|
| 98 |
this.configure(config); |
| 99 |
} |
| 100 |
|
| 101 |
after() { |
| 102 |
for (const tag of this.tags) { |
| 103 |
tag.after?.(this); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
before() { |
| 108 |
for (const tag of this.tags) { |
| 109 |
tag.before?.(this); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @param {JobConfig & { tags?: Iterable<Tag> }} config |
| 115 |
*/ |
| 116 |
configure({ tags, ...config }) { |
| 117 |
// Assigns and validates job config |
| 118 |
$assign(this.config, config); |
| 119 |
validateConfig(this.config); |
| 120 |
|
| 121 |
// Add tags |
| 122 |
applyTags(this, tags); |
| 123 |
} |
| 124 |
|
| 125 |
minimize() { |
| 126 |
this[S_MINIMIZED] = true; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @returns {boolean} |
| 131 |
*/ |
| 132 |
willRunAgain() { |
| 133 |
return this.runCount < (this.config.multi || 0) || this.parent?.willRunAgain(); |
| 134 |
} |
| 135 |
} |