TMT-Demo/tests/features/conversions.test.ts
thepaperpilot 83d41428eb Feature rewrite
- Removed `jsx()` and `JSXFunction`. You can now use `JSX.Element` like any other `Computable` value
- `joinJSX` now always requires a joiner. Just pass the array of elements or wrap them in `<>` and `</>` if there's no joiner
- Removed `coerceComponent`, `computeComponent`, and `computeOptionalComponent`; just use the `render` function now
- It's recommended to now do `<MyComponent />` instead of `<component :is="myComponent" />`
- All features no longer take the options as a type parameter, and all generic forms have been removed as a result
- Fixed `forceHideGoBack` not being respected
- Removed `deepUnref` as now things don't get unreffed before being passed into vue components by default
- Moved MarkNode to new wrapper, and removed existing `mark` properties
- Moved Tooltip to new wrapper, and made it take an options function instead of raw object
- VueFeature component now wraps all vue features, and applies styling, classes, and visibility in the wrapping div. It also adds the Node component so features don't need to
- `mergeAdjacent` now works with grids (perhaps should've used scss to reduce the amount of css this took)
- `CoercableComponent` renamed to `Renderable` since it should be used with `render`
- Replaced `isCoercableComponent` with `isJSXElement`
- Replaced `Computable` and `ProcessedComputable` with the vue built-ins `MaybeRefOrGetter` and `MaybeRef`
- `convertComputable` renamed to `processGetter`
- Also removed `GetComputableTypeWithDefault` and `GetComputableType`, which can similarly be replaced
- `dontMerge` is now a property on rows and columns rather than an undocumented css class you'd have to include on every feature within the row or column
- Fixed saves manager not being imported in addiction warning component
- Created `vueFeatureMixin` for simplifying the vue specific parts of a feature. Passes the component's properties in explicitly and directly from the feature itself
- All features should now return an object that includes props typed to omit the options object and satisfies the feature. This will ensure type correctness and pass-through custom properties. (see existing features for more thorough examples of changes)
- Replaced decorators with mixins, which won't require casting. Bonus amount decorators converted into generic bonus amount mixin. Removed effect decorator
- All `render` functions now return `JSX.Element`. The `JSX` variants (e.g. `renderJSX`) (except `joinJSX`) have been removed
- Moved all features that use the clickable component into the clickable folder
- Removed `small` property from clickable, since its a single css rule (`min-height: unset`) (you could add a small css class and pass small to any vue feature's classes property, though)
- Upgrades now use the clickable component
- Added ConversionType symbol
- Removed setDefault, just use `??=`
- Added isType function that uses a type symbol to check
- General cleanup
2024-11-29 08:02:24 -06:00

518 lines
20 KiB
TypeScript

import {
Conversion,
createCumulativeConversion,
createIndependentConversion,
setupPassiveGeneration
} from "features/conversion";
import { createResource, Resource } from "features/resources/resource";
import { InvertibleIntegralFormula } from "game/formulas/types";
import { createLayer, Layer } from "game/layers";
import Decimal from "util/bignum";
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
import { ref, unref } from "vue";
import "../utils";
describe("Creating conversion", () => {
let baseResource: Resource;
let gainResource: Resource;
let formula: (x: InvertibleIntegralFormula) => InvertibleIntegralFormula;
beforeEach(() => {
baseResource = createResource(ref(40));
gainResource = createResource(ref(1));
formula = x => x.div(10).sqrt();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("Cumulative conversion", () => {
describe("Calculates currentGain correctly", () => {
let conversion: Conversion;
beforeEach(() => {
conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula
}));
});
test("Exactly enough", () => {
baseResource.value = Decimal.pow(100, 2).times(10);
expect(unref(conversion.currentGain)).compare_tolerance(100);
});
test("Just under", () => {
baseResource.value = Decimal.pow(100, 2).times(10).sub(1);
expect(unref(conversion.currentGain)).compare_tolerance(99);
});
test("Just over", () => {
baseResource.value = Decimal.pow(100, 2).times(10).add(1);
expect(unref(conversion.currentGain)).compare_tolerance(100);
});
test("Zero", () => {
baseResource.value = Decimal.dZero;
expect(unref(conversion.currentGain)).compare_tolerance(0);
});
});
describe("Calculates actualGain correctly", () => {
let conversion: Conversion;
beforeEach(() => {
conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula
}));
});
test("Exactly enough", () => {
baseResource.value = Decimal.pow(100, 2).times(10);
expect(unref(conversion.actualGain)).compare_tolerance(100);
});
test("Just under", () => {
baseResource.value = Decimal.pow(100, 2).times(10).sub(1);
expect(unref(conversion.actualGain)).compare_tolerance(99);
});
test("Just over", () => {
baseResource.value = Decimal.pow(100, 2).times(10).add(1);
expect(unref(conversion.actualGain)).compare_tolerance(100);
});
test("Zero", () => {
baseResource.value = Decimal.dZero;
expect(unref(conversion.actualGain)).compare_tolerance(0);
});
});
describe("Calculates currentAt correctly", () => {
let conversion: Conversion;
beforeEach(() => {
conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula
}));
});
test("Exactly enough", () => {
baseResource.value = Decimal.pow(100, 2).times(10);
expect(unref(conversion.currentAt)).compare_tolerance(
Decimal.pow(100, 2).times(10)
);
});
test("Just under", () => {
baseResource.value = Decimal.pow(100, 2).times(10).sub(1);
expect(unref(conversion.currentAt)).compare_tolerance(Decimal.pow(99, 2).times(10));
});
test("Just over", () => {
baseResource.value = Decimal.pow(100, 2).times(10).add(1);
expect(unref(conversion.currentAt)).compare_tolerance(
Decimal.pow(100, 2).times(10)
);
});
test("Zero", () => {
baseResource.value = Decimal.dZero;
expect(unref(conversion.currentAt)).compare_tolerance(0);
});
});
describe("Calculates nextAt correctly", () => {
let conversion: Conversion;
beforeEach(() => {
conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula
}));
});
test("Exactly enough", () => {
baseResource.value = Decimal.pow(100, 2).times(10);
expect(unref(conversion.nextAt)).compare_tolerance(Decimal.pow(101, 2).times(10));
});
test("Just under", () => {
baseResource.value = Decimal.pow(100, 2).times(10).sub(1);
expect(unref(conversion.nextAt)).compare_tolerance(Decimal.pow(100, 2).times(10));
});
test("Just over", () => {
baseResource.value = Decimal.pow(100, 2).times(10).add(1);
expect(unref(conversion.nextAt)).compare_tolerance(Decimal.pow(101, 2).times(10));
});
test("Zero", () => {
baseResource.value = Decimal.dZero;
expect(unref(conversion.nextAt)).compare_tolerance(Decimal.dTen);
});
});
test("Converts correctly", () => {
const conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula
}));
conversion.convert();
expect(baseResource.value).compare_tolerance(0);
expect(gainResource.value).compare_tolerance(3);
});
describe("Obeys buy max", () => {
test("buyMax = false", () => {
const conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula,
buyMax: false
}));
expect(unref(conversion.actualGain)).compare_tolerance(1);
});
test("buyMax = true", () => {
const conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula,
buyMax: true
}));
expect(unref(conversion.actualGain)).compare_tolerance(2);
});
});
test("Spends correctly", () => {
const conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula
}));
conversion.convert();
expect(baseResource.value).compare_tolerance(0);
});
test("Calls onConvert", () => {
const onConvert = vi.fn();
const conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula,
onConvert
}));
conversion.convert();
expect(onConvert).toHaveBeenCalled();
});
});
describe("Independent conversion", () => {
describe("Calculates currentGain correctly", () => {
let conversion: Conversion;
beforeEach(() => {
conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
buyMax: true
}));
});
test("Exactly enough", () => {
baseResource.value = Decimal.pow(100, 2).times(10);
expect(unref(conversion.currentGain)).compare_tolerance(100);
});
test("Just under", () => {
baseResource.value = Decimal.pow(100, 2).times(10).sub(1);
expect(unref(conversion.currentGain)).compare_tolerance(99);
});
test("Just over", () => {
baseResource.value = Decimal.pow(100, 2).times(10).add(1);
expect(unref(conversion.currentGain)).compare_tolerance(100);
});
test("Zero", () => {
baseResource.value = Decimal.dZero;
expect(unref(conversion.currentGain)).compare_tolerance(1);
});
});
describe("Calculates actualGain correctly", () => {
let conversion: Conversion;
beforeEach(() => {
conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
buyMax: true
}));
});
test("Exactly enough", () => {
baseResource.value = Decimal.pow(100, 2).times(10);
expect(unref(conversion.actualGain)).compare_tolerance(99);
});
test("Just under", () => {
baseResource.value = Decimal.pow(100, 2).times(10).sub(1);
expect(unref(conversion.actualGain)).compare_tolerance(98);
});
test("Just over", () => {
baseResource.value = Decimal.pow(100, 2).times(10).add(1);
expect(unref(conversion.actualGain)).compare_tolerance(99);
});
test("Zero", () => {
baseResource.value = Decimal.dZero;
expect(unref(conversion.actualGain)).compare_tolerance(0);
});
});
describe("Calculates currentAt correctly", () => {
let conversion: Conversion;
beforeEach(() => {
conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
buyMax: true
}));
});
test("Exactly enough", () => {
baseResource.value = Decimal.pow(100, 2).times(10);
expect(unref(conversion.currentAt)).compare_tolerance(
Decimal.pow(100, 2).times(10)
);
});
test("Just under", () => {
baseResource.value = Decimal.pow(100, 2).times(10).sub(1);
expect(unref(conversion.currentAt)).compare_tolerance(Decimal.pow(99, 2).times(10));
});
test("Just over", () => {
baseResource.value = Decimal.pow(100, 2).times(10).add(1);
expect(unref(conversion.currentAt)).compare_tolerance(
Decimal.pow(100, 2).times(10)
);
});
test("Zero", () => {
baseResource.value = Decimal.dZero;
expect(unref(conversion.currentAt)).compare_tolerance(Decimal.pow(1, 2).times(10));
});
});
describe("Calculates nextAt correctly", () => {
let conversion: Conversion;
beforeEach(() => {
conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
buyMax: true
}));
});
test("Exactly enough", () => {
baseResource.value = Decimal.pow(100, 2).times(10);
expect(unref(conversion.nextAt)).compare_tolerance(Decimal.pow(101, 2).times(10));
});
test("Just under", () => {
baseResource.value = Decimal.pow(100, 2).times(10).sub(1);
expect(unref(conversion.nextAt)).compare_tolerance(Decimal.pow(100, 2).times(10));
});
test("Just over", () => {
baseResource.value = Decimal.pow(100, 2).times(10).add(1);
expect(unref(conversion.nextAt)).compare_tolerance(Decimal.pow(101, 2).times(10));
});
test("Zero", () => {
baseResource.value = Decimal.dZero;
expect(unref(conversion.nextAt)).compare_tolerance(Decimal.pow(2, 2).times(10));
});
});
test("Converts correctly", () => {
const conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula
}));
conversion.convert();
expect(baseResource.value).compare_tolerance(0);
expect(gainResource.value).compare_tolerance(2);
});
describe("Obeys buy max", () => {
test("buyMax = false", () => {
const conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
buyMax: false
}));
baseResource.value = 90;
expect(unref(conversion.actualGain)).compare_tolerance(1);
});
test("buyMax = true", () => {
const conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
buyMax: true
}));
baseResource.value = 90;
expect(unref(conversion.actualGain)).compare_tolerance(2);
});
});
test("Spends correctly", () => {
const conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula
}));
conversion.convert();
expect(baseResource.value).compare_tolerance(0);
});
test("Calls onConvert", () => {
const onConvert = vi.fn();
const conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
onConvert
}));
conversion.convert();
expect(onConvert).toHaveBeenCalled();
});
});
describe("Custom conversion", () => {
describe("Custom cumulative", () => {
let conversion: Conversion;
const convert = vi.fn();
const spend = vi.fn();
const onConvert = vi.fn();
beforeAll(() => {
conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula,
currentGain() {
return 10;
},
actualGain() {
return 5;
},
currentAt() {
return 100;
},
nextAt() {
return 1000;
},
convert,
spend,
onConvert
}));
});
afterEach(() => {
vi.resetAllMocks();
});
test("Calculates currentGain correctly", () => {
expect(unref(conversion.currentGain)).compare_tolerance(10);
});
test("Calculates actualGain correctly", () => {
expect(unref(conversion.actualGain)).compare_tolerance(5);
});
test("Calculates currentAt correctly", () => {
expect(unref(conversion.currentAt)).compare_tolerance(100);
});
test("Calculates nextAt correctly", () => {
expect(unref(conversion.nextAt)).compare_tolerance(1000);
});
test("Calls convert", () => {
conversion.convert();
expect(convert).toHaveBeenCalled();
});
test("Calls spend and onConvert", () => {
conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula,
spend,
onConvert
}));
conversion.convert();
expect(spend).toHaveBeenCalled();
expect(spend).toHaveBeenCalledWith(expect.compare_tolerance(2));
expect(onConvert).toHaveBeenCalled();
expect(onConvert).toHaveBeenCalledWith(expect.compare_tolerance(2));
});
});
describe("Custom independent", () => {
let conversion: Conversion;
const convert = vi.fn();
const spend = vi.fn();
const onConvert = vi.fn();
beforeAll(() => {
conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
currentGain() {
return 10;
},
actualGain() {
return 5;
},
currentAt() {
return 100;
},
nextAt() {
return 1000;
},
convert,
spend,
onConvert
}));
});
afterEach(() => {
vi.resetAllMocks();
});
test("Calculates currentGain correctly", () => {
expect(unref(conversion.currentGain)).compare_tolerance(10);
});
test("Calculates actualGain correctly", () => {
expect(unref(conversion.actualGain)).compare_tolerance(5);
});
test("Calculates currentAt correctly", () => {
expect(unref(conversion.currentAt)).compare_tolerance(100);
});
test("Calculates nextAt correctly", () => {
expect(unref(conversion.nextAt)).compare_tolerance(1000);
});
test("Calls convert", () => {
conversion.convert();
expect(convert).toHaveBeenCalled();
});
test("Calls spend and onConvert", () => {
conversion = createIndependentConversion(() => ({
baseResource,
gainResource,
formula,
spend,
onConvert
}));
conversion.convert();
expect(spend).toHaveBeenCalled();
expect(spend).toHaveBeenCalledWith(expect.compare_tolerance(1));
expect(onConvert).toHaveBeenCalled();
expect(onConvert).toHaveBeenCalledWith(expect.compare_tolerance(1));
});
});
});
});
describe("Passive generation", () => {
let baseResource: Resource;
let gainResource: Resource;
let formula: (x: InvertibleIntegralFormula) => InvertibleIntegralFormula;
let conversion: Conversion;
let layer: Layer;
beforeEach(() => {
baseResource = createResource(ref(10));
gainResource = createResource(ref(1));
formula = x => x.div(10).sqrt();
conversion = createCumulativeConversion(() => ({
baseResource,
gainResource,
formula
}));
layer = createLayer("dummy", () => ({ display: "" }));
});
test("Rate is 0", () => {
setupPassiveGeneration(layer, conversion, 0);
layer.emit("preUpdate", 1);
expect(gainResource.value).compare_tolerance(1);
});
test("Rate is 1", () => {
setupPassiveGeneration(layer, conversion);
layer.emit("preUpdate", 1);
expect(gainResource.value).compare_tolerance(2);
});
test("Rate is 100", () => {
setupPassiveGeneration(layer, conversion, () => 100);
layer.emit("preUpdate", 1);
expect(gainResource.value).compare_tolerance(101);
});
test("Obeys cap", () => {
setupPassiveGeneration(layer, conversion, 100, () => 100);
layer.emit("preUpdate", 1);
expect(gainResource.value).compare_tolerance(100);
});
});