1. A code to be tested. This code comes from a simple application that supports users to view and adjust production plans. Its (slightly rough) interface looks like the picture below. as shown in Figure 1

2. The final implementation of the example code is as follows, the productionplan.html file code of the entrance is as follows
构筑测试体系 - 待测试的示例代码
3. The code of the productionPlan.js file introduced is as follows
function sampleProvinceData() {
return {
name: "Asia",
producers: [
{name: "Byzantium", cost: 10, production: 9},
{name: "Attalia", cost: 12, production: 10},
{name: "Sinope", cost: 10, production: 6},
],
demand: 30,
price: 20
};
}
class Province {
constructor(doc) {
this._name = doc.name;
this._producers = [];
this._totalProduction = 0;
this._demand = doc.demand;
this._price = doc.price;
doc.producers.forEach(d => this.addProducer(new Producer(this, d)));
}
addProducer(arg) {
this._producers.push(arg);
this._totalProduction += arg.production;
}
get name() {
return this._name;
}
get producers() {
return this._producers.slice();
}
get totalProduction() {
return this._totalProduction;
}
set totalProduction(arg) {
this._totalProduction = arg;
}
get demand() {
return this._demand;
}
set demand(arg) {
this._demand = parseInt(arg);
}
get price() {
return this._price;
}
set price(arg) {
this._price = parseInt(arg);
}
get shortfall() {
return this._demand - this.totalProduction;
}
get profit() {
return this.demandValue - this.demandCost;
}
get demandCost() {
let remainingDemand = this.demand;
let result = 0;
this.producers
.sort((a, b) => a.cost - b.cost)
.forEach(p => {
const contribution = Math.min(remainingDemand, p.production);
remainingDemand -= contribution;
result += contribution * p.cost;
});
return result;
}
get demandValue() {
return this.satisfiedDemand * this.price;
}
get satisfiedDemand() {
return Math.min(this._demand, this.totalProduction);
}
}
class Producer {
constructor(aProvince, data) {
this._province = aProvince;
this._cost = data.cost;
this._name = data.name;
this._production = data.production || 0;
}
get name() {
return this._name;
}
get cost() {
return this._cost;
}
set cost(arg) {
this._cost = parseInt(arg);
}
get production() {
return this._production;
}
set production(amountStr) {
const amount = parseInt(amountStr);
const newProduction = Number.isNaN(amount) ? 0 : amount;
this._province.totalProduction += newProduction - this._production;
this._production = newProduction;
}
}
const asia = new Province(sampleProvinceData());
console.log(asia.shortfall);
3. The code const asia = new province(sampleprovincedata()); Reference: 4.3 The first test example. as shown in Figure 2

4. Open in the browser:http://localhost/refactoring/4/productionPlan.html, check the console output: 5, in line with expectations. as shown in Figure 3

Need long-term technical maintenance or remote troubleshooting?
I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.
If your project is facing any of the following issues, we can start with a small troubleshooting task first:
- ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
- ✅ Go / Gin backend APIs that need troubleshooting or optimization
- ✅ Slow, broken, or unstable WordPress websites
- ✅ Nginx / MySQL / Redis / Linux server issues
- ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
- ✅ Long-term remote technical support or part-time maintenance
More details: About Me & Collaboration
WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

Leave a Reply