审查生成的运费规格

来自 可执行规格
Node 24 进阶 10分钟 找出 4处问题

在智能体把这段生成规格用作验收证据前进行审查。

规定小计必须是以整数分表示的非负整数;法国标准配送在 5,000 分以下收取 500 分,达到或超过 5,000 分免费;法国加急配送收取 900 分;非法国配送收取 1,200 分;国家代码缺失或小计无效时必须拒绝。

JavaScript
import assert from "node:assert/strict";

function shippingFeeCents(order) {
  const { subtotalCents, country, expedited = false } = order;
  if (subtotalCents < 0) throw new RangeError("negative subtotal");
  if (country !== "FR") return 1200;
  if (expedited) return 900;
  return subtotalCents > 5000 ? 0 : 500;
}

const examples = [
  [{ subtotalCents: 6000, country: "FR" }, 0],
  [{ subtotalCents: 2000, country: "FR" }, 500],
  [{ subtotalCents: 8000, country: "FR", expedited: true }, 900],
];

for (const example of examples) {
  const [input, expected] = example;
  assert.equal(shippingFeeCents(input), expected);
}

for (let subtotal = 0; subtotal < 10_000_000; subtotal += 1) {
  assert.ok(shippingFeeCents({ subtotalCents: subtotal, country: "FR" }) >= 0);
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误