mirror of
https://github.com/MadsLorentzen/ai-job-search.git
synced 2026-09-17 08:36:25 +00:00
fix(portal-clis): accept full posting URLs in jobbank, jobdanmark, and jobnet detail commands (#430)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { defineCommand, option } from "@bunli/core"
|
||||
import { z } from "zod"
|
||||
import { fetchWithUA, parseJobPostingJsonLd, writeError, BASE_URL } from "../helpers.js"
|
||||
import { fetchWithUA, normalizeJobId, parseJobPostingJsonLd, writeError, BASE_URL } from "../helpers.js"
|
||||
|
||||
export const detail = defineCommand({
|
||||
name: "detail",
|
||||
@@ -13,12 +13,18 @@ export const detail = defineCommand({
|
||||
handler: async ({ positional, flags, signal }) => {
|
||||
if (signal.aborted) return
|
||||
|
||||
const id = positional[0]
|
||||
if (!id) {
|
||||
const rawId = positional[0]
|
||||
if (!rawId) {
|
||||
writeError("Job ID is required", "MISSING_REQUIRED")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const id = normalizeJobId(rawId)
|
||||
if (!id) {
|
||||
writeError(`Could not extract job ID from "${rawId}"`, "BAD_ID")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const url = `${BASE_URL}/job/${id}/`
|
||||
|
||||
try {
|
||||
|
||||
@@ -159,10 +159,16 @@ export function parseRssDescription(desc: string): ParsedDescription {
|
||||
return { jobType, company, location, deadline }
|
||||
}
|
||||
|
||||
export function normalizeJobId(input: string): string | null {
|
||||
const trimmed = input.trim()
|
||||
if (/^\d+$/.test(trimmed)) return trimmed
|
||||
const match = trimmed.match(/\/job\/(\d+)(?:\/|$|\?|#)/)
|
||||
return match ? match[1] : null
|
||||
}
|
||||
|
||||
export function extractJobIdFromUrl(url: string): string {
|
||||
// URL format: https://jobbank.dk/job/{id}/{company-slug}/{title-slug}
|
||||
const match = url.match(/\/job\/(\d+)\//)
|
||||
return match ? match[1] : ""
|
||||
return normalizeJobId(url) ?? ""
|
||||
}
|
||||
|
||||
function findJobPosting(value: unknown): Record<string, unknown> | null {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { normalizeJobId } from "../src/helpers.js"
|
||||
import { runCLI } from "./helpers.js"
|
||||
|
||||
describe("jobbank-search normalizeJobId", () => {
|
||||
test("accepts bare numeric ID", () => {
|
||||
expect(normalizeJobId("304212")).toBe("304212")
|
||||
expect(normalizeJobId(" 12345 ")).toBe("12345")
|
||||
})
|
||||
|
||||
test("extracts ID from full URL with trailing slash", () => {
|
||||
expect(normalizeJobId("https://jobbank.dk/job/304212/")).toBe("304212")
|
||||
})
|
||||
|
||||
test("extracts ID from full URL without trailing slash", () => {
|
||||
expect(normalizeJobId("https://jobbank.dk/job/304212")).toBe("304212")
|
||||
})
|
||||
|
||||
test("extracts ID from full URL with company/role slug segments", () => {
|
||||
expect(normalizeJobId("https://jobbank.dk/job/304212/acme-corp/software-developer")).toBe("304212")
|
||||
expect(normalizeJobId("https://jobbank.dk/job/304212/acme-corp/software-developer/")).toBe("304212")
|
||||
})
|
||||
|
||||
test("extracts ID from URL with query parameters and hash fragments", () => {
|
||||
expect(normalizeJobId("https://jobbank.dk/job/304212?ref=search&page=1")).toBe("304212")
|
||||
expect(normalizeJobId("https://jobbank.dk/job/304212#apply")).toBe("304212")
|
||||
})
|
||||
|
||||
test("rejects invalid non-numeric strings and unrelated URLs", () => {
|
||||
expect(normalizeJobId("abc")).toBeNull()
|
||||
expect(normalizeJobId("https://example.com/other/12345")).toBeNull()
|
||||
expect(normalizeJobId("")).toBeNull()
|
||||
})
|
||||
|
||||
test("CLI detail command rejects invalid ID format with BAD_ID", async () => {
|
||||
const result = await runCLI(["detail", "invalid-id-format"])
|
||||
expect(result.exitCode).toBe(1)
|
||||
const err = JSON.parse(result.stderr)
|
||||
expect(err.code).toBe("BAD_ID")
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user