Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/bun-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
bun-version: latest

- name: Install dependencies
run: bun install
run: bun install && cd cf-proxy && bun install && cd ..

- name: Cache setup artifacts
id: cache-setup
Expand Down
5 changes: 5 additions & 0 deletions routes/api/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default withWinterSpec({
limit: z.string().optional(),
is_basic: z.boolean().optional(),
is_preferred: z.boolean().optional(),
is_extended_promotional: z.boolean().optional(),
}),
jsonResponse: z.any(),
} as const)(async (req, ctx) => {
Expand All @@ -64,6 +65,9 @@ export default withWinterSpec({
if (req.query.is_preferred) {
query = query.where("preferred", "=", 1)
}
if (req.query.is_extended_promotional) {
query = query.where("preferred", "=", 1).where("basic", "=", 0)
}

const baseQuery = query
let fallbackLikeTokens: string[] = []
Expand Down Expand Up @@ -189,6 +193,7 @@ export default withWinterSpec({
package: c.package,
is_basic: Boolean(c.basic),
is_preferred: Boolean(c.preferred),
is_extended_promotional: Boolean(c.preferred) && !c.basic,
description: c.description,
stock: c.stock,
price: extractSmallQuantityPrice(c.price),
Expand Down
19 changes: 17 additions & 2 deletions routes/components/list.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { sql } from "kysely"
import { Table } from "lib/ui/Table"
import { ExpressionBuilder } from "kysely"
import { withWinterSpec } from "lib/with-winter-spec"
import { z } from "zod"

Expand All @@ -23,6 +21,7 @@ export default withWinterSpec({
search: z.string().optional(),
is_basic: z.boolean().optional(),
is_preferred: z.boolean().optional(),
is_extended_promotional: z.boolean().optional(),
}),
jsonResponse: z.any(),
} as const)(async (req, ctx) => {
Expand All @@ -39,6 +38,7 @@ export default withWinterSpec({
"price",
"extra",
"basic",
"preferred",
])
.limit(limit)
.orderBy("stock", "desc")
Expand All @@ -58,6 +58,9 @@ export default withWinterSpec({
if (req.query.is_preferred) {
query = query.where("preferred", "=", 1)
}
if (req.query.is_extended_promotional) {
query = query.where("preferred", "=", 1).where("basic", "=", 0)
}

if (req.query.search) {
const search = req.query.search // TypeScript now knows this is defined within this block
Expand All @@ -82,6 +85,7 @@ export default withWinterSpec({
package: c.package,
is_basic: Boolean(c.basic),
is_preferred: Boolean(c.preferred),
is_extended_promotional: Boolean(c.preferred) && !c.basic,
description: c.description,
stock: c.stock,
price: extractSmallQuantityPrice(c.price),
Expand Down Expand Up @@ -126,6 +130,17 @@ export default withWinterSpec({
/>
</label>
</div>
<div>
<label>
Extended Promotional:
<input
type="checkbox"
name="is_extended_promotional"
value="true"
checked={req.query.is_extended_promotional}
/>
</label>
</div>
<button type="submit">Filter</button>
</form>

Expand Down
29 changes: 29 additions & 0 deletions tests/routes/api/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,32 @@ test("GET /api/search supports '0402 LED'", async () => {
expect(res.data.components.every((c: any) => c.package === "0402")).toBe(true)
expect(res.data.components.some((c: any) => c.lcsc === 965793)).toBe(true)
})

test("GET /api/search returns is_extended_promotional field", async () => {
const { axios } = await getTestServer()
const res = await axios.get("/api/search?q=resistor&limit=10")

expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)
expect(res.data.components.length).toBeGreaterThan(0)

const component = res.data.components[0]
expect(component).toHaveProperty("is_extended_promotional")
expect(typeof component.is_extended_promotional).toBe("boolean")
})

test("GET /api/search with is_extended_promotional=true returns only extended promotional components", async () => {
const { axios } = await getTestServer()
const res = await axios.get(
"/api/search?is_extended_promotional=true&limit=50",
)

expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)

for (const component of res.data.components) {
expect(component.is_preferred).toBe(true)
expect(component.is_basic).toBe(false)
expect(component.is_extended_promotional).toBe(true)
}
})
31 changes: 30 additions & 1 deletion tests/routes/components/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from "bun:test"
import { expect, test } from "bun:test"
import { getTestServer } from "tests/fixtures/get-test-server"

test("GET /components/list with json param returns component data", async () => {
Expand All @@ -7,3 +7,32 @@ test("GET /components/list with json param returns component data", async () =>
expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)
})

test("GET /components/list returns is_extended_promotional field", async () => {
const { axios } = await getTestServer()
const res = await axios.get("/components/list?json=true")

expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)
expect(res.data.components.length).toBeGreaterThan(0)

const component = res.data.components[0]
expect(component).toHaveProperty("is_extended_promotional")
expect(typeof component.is_extended_promotional).toBe("boolean")
})

test("GET /components/list with is_extended_promotional=true returns only extended promotional components", async () => {
const { axios } = await getTestServer()
const res = await axios.get(
"/components/list?json=true&is_extended_promotional=true",
)

expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)

for (const component of res.data.components) {
expect(component.is_preferred).toBe(true)
expect(component.is_basic).toBe(false)
expect(component.is_extended_promotional).toBe(true)
}
})
Loading