diff --git a/core/src/duplicates.test.ts b/core/src/duplicates.test.ts index 9850c5e..848e7f9 100644 --- a/core/src/duplicates.test.ts +++ b/core/src/duplicates.test.ts @@ -206,6 +206,62 @@ describe('findApplicationMatch', () => { }); }); +describe('form apply-page addresses', () => { + const stored = 'https://jobs.lever.co/example/abc'; + const data = snap({ + jobs: [{ id: 'j1', createdAt: `2026-06-12${AT}`, canonicalUrl: stored }], + applications: [{ id: 'a1', createdAt: `2026-06-13${AT}`, jobId: 'j1' }], + }); + + it('findApplicationMatch matches the /apply address of a stored job', () => { + expect( + findApplicationMatch(data, { canonicalUrl: `${stored}/apply` }), + ).toEqual({ kind: 'already_applied', applicationId: 'a1' }); + expect( + findApplicationMatch(data, { + canonicalUrl: 'https://jobs.lever.co/example/other/apply', + }), + ).toBeUndefined(); + }); + + it('matches the Ashby /application address of a stored job', () => { + const ashby = 'https://jobs.ashbyhq.com/example/abc'; + const ashbyData = snap({ + jobs: [{ id: 'j2', createdAt: `2026-06-12${AT}`, canonicalUrl: ashby }], + applications: [{ id: 'a2', createdAt: `2026-06-13${AT}`, jobId: 'j2' }], + }); + expect( + findApplicationMatch(ashbyData, { canonicalUrl: `${ashby}/application` }), + ).toEqual({ kind: 'already_applied', applicationId: 'a2' }); + expect( + repostFact( + ashbyData, + { canonicalUrl: `${ashby}/application` }, + '2026-11-15', + )?.firstSavedOn, + ).toBe('2026-06-12'); + expect( + findApplicationMatch(ashbyData, { + canonicalUrl: 'https://jobs.ashbyhq.com/example/zzz/application', + }), + ).toBeUndefined(); + }); + + it('repostFact matches the /apply address of a stored job', () => { + expect( + repostFact(data, { canonicalUrl: `${stored}/apply` }, '2026-11-15') + ?.firstSavedOn, + ).toBe('2026-06-12'); + expect( + repostFact( + data, + { canonicalUrl: 'https://jobs.lever.co/example/other/apply' }, + '2026-11-15', + ), + ).toBeUndefined(); + }); +}); + describe('repostFact', () => { const job = (id: string, day: string, extra: Partial = {}): Job => ({ id, diff --git a/core/src/duplicates.ts b/core/src/duplicates.ts index 4259da0..735c4cf 100644 --- a/core/src/duplicates.ts +++ b/core/src/duplicates.ts @@ -1,5 +1,6 @@ import { normalizeName } from './application-ops'; import { isLocalDate, toLocalDate } from './dates'; +import { formUrlKey } from './form-link'; import type { Application, Id, Job, LocalDate, Snapshot } from './types'; import { canonicalizeUrl } from './url'; @@ -45,6 +46,14 @@ function safeCanonical(url: string): string | undefined { } } +// Equal, or both the same posting once a form's apply segment is dropped. +function urlsMatch(have: string | undefined, want: string): boolean { + if (have === undefined) return false; + if (have === want) return true; + const key = formUrlKey(want); + return key !== undefined && key === formUrlKey(have); +} + function applicationCanonicalUrl( app: Application, jobsById: Map, @@ -110,7 +119,7 @@ export function findApplicationMatch( const appTitle = nameKey(app.title); if ( wantUrl !== undefined && - url === wantUrl && + urlsMatch(url, wantUrl) && !contradicts(wantCompany, company, wantTitle, appTitle) ) { if ( @@ -169,7 +178,7 @@ export function repostFact( const jobTitle = nameKey(job.title); const sameUrl = wantUrl !== undefined && - str(job.canonicalUrl) === wantUrl && + urlsMatch(str(job.canonicalUrl), wantUrl) && !contradicts(wantCompany, jobCompany, wantTitle, jobTitle); const sameName = wantCompany !== undefined && diff --git a/core/src/form-application.test.ts b/core/src/form-application.test.ts index 6704ecc..62f58e7 100644 --- a/core/src/form-application.test.ts +++ b/core/src/form-application.test.ts @@ -50,6 +50,49 @@ function stores(ops: WriteOp[], store: string): WriteOp[] { return ops.filter((o) => o.kind === 'put' && o.store === store); } +describe('newApplicationFromFormOps facts', () => { + const archivedJob: Job = { + id: 'oldjob', + source: 'paste', + title: 'Dev', + companyName: 'Acme', + canonicalUrl: 'https://jobs.lever.co/acme/1', + createdAt: '2025-12-01T00:00:00.000Z', + }; + + it('reports a duplicate and a repost of an earlier job at the form key', () => { + const data: Snapshot = { + ...emptySnapshot(), + jobs: [archivedJob], + applications: [ + { + id: 'oldapp', + jobId: 'oldjob', + title: 'Dev', + createdAt: '2025-12-01T00:00:00.000Z', + }, + ], + }; + const built = newApplicationFromFormOps( + data, + { source: 'paste', url: URL, title: 'Dev', companyName: 'Acme' }, + makeDeps(), + ); + expect(built.match).toBe('already_applied'); + expect(built.repost).toMatch(/^You saved this posting on /); + }); + + it('reports nothing for a new posting and never counts itself', () => { + const built = newApplicationFromFormOps( + emptySnapshot(), + { source: 'paste', url: URL, title: 'Dev', companyName: 'Acme' }, + makeDeps(), + ); + expect(built.match).toBeUndefined(); + expect(built.repost).toBeUndefined(); + }); +}); + describe('newApplicationFromFormOps', () => { it('makes one job and one linked interested application', () => { const { ops, applicationId, record } = newApplicationFromFormOps( diff --git a/core/src/form-application.ts b/core/src/form-application.ts index 28c8c74..f21f9fb 100644 --- a/core/src/form-application.ts +++ b/core/src/form-application.ts @@ -1,3 +1,10 @@ +import { toLocalDate } from './dates'; +import { + findApplicationMatch, + repostFact, + type ApplicationMatch, + type JobCandidate, +} from './duplicates'; import type { FillResult } from './fill/result'; import { fillRecordOps, type FillRecord } from './fill-records'; import { formUrlKey, linkFormOps } from './form-link'; @@ -48,7 +55,13 @@ export function newApplicationFromFormOps( input: JobInput, deps: Deps, held?: FillResult, -): { ops: WriteOp[]; applicationId: Id; record?: FillRecord } { +): { + ops: WriteOp[]; + applicationId: Id; + record?: FillRecord; + match?: ApplicationMatch['kind']; + repost?: string; +} { const saved = saveJobOps(input, deps); const jobPut = saved.ops[0]; if (jobPut?.kind !== 'put' || jobPut.store !== 'jobs') { @@ -68,7 +81,26 @@ export function newApplicationFromFormOps( const ops: WriteOp[] = [...created.ops]; ops.splice(companyIndex + 1, 0, jobOp); - if (held === undefined) return { ops, applicationId: created.applicationId }; + // Facts only inform, as in Save job: computed on the data before this write, + // for a candidate stored the way the job is (form key as its canonical URL). + const candidate: JobCandidate = {}; + if (job.canonicalUrl !== undefined) candidate.canonicalUrl = job.canonicalUrl; + if (job.companyName !== undefined) candidate.companyName = job.companyName; + if (job.title !== undefined) candidate.title = job.title; + const match = findApplicationMatch(data, candidate)?.kind; + const repost = repostFact( + data, + { ...candidate, excludeJobId: job.id }, + toLocalDate(new Date(deps.now())), + )?.text; + const facts = { + ...(match !== undefined ? { match } : {}), + ...(repost !== undefined ? { repost } : {}), + }; + + if (held === undefined) { + return { ops, applicationId: created.applicationId, ...facts }; + } const next: Snapshot = { ...data, @@ -88,5 +120,5 @@ export function newApplicationFromFormOps( } const record = fillRecordOps(next, held, created.applicationId, deps); ops.push(...record.ops); - return { ops, applicationId: created.applicationId, record }; + return { ops, applicationId: created.applicationId, record, ...facts }; } diff --git a/core/src/form-link.test.ts b/core/src/form-link.test.ts index 6b2875b..80c5b64 100644 --- a/core/src/form-link.test.ts +++ b/core/src/form-link.test.ts @@ -26,6 +26,15 @@ function app(id: string, day: string, extra: object = {}): Application { const KEY = 'https://example.com/jobs/1'; describe('formUrlKey', () => { + it('ignores an address longer than the capture cap', () => { + const long = `https://example.com/${'a'.repeat(8_001 - 20)}`; + expect(long).toHaveLength(8_001); + expect(formUrlKey(long)).toBeUndefined(); + expect(formUrlKey('https://example.com/jobs/1')).toBe( + 'https://example.com/jobs/1', + ); + }); + it('strips the Lever apply segment', () => { expect(formUrlKey('https://jobs.lever.co/acme/123/apply')).toBe( formUrlKey('https://jobs.lever.co/acme/123'), diff --git a/core/src/form-link.ts b/core/src/form-link.ts index bdc0310..7e46f34 100644 --- a/core/src/form-link.ts +++ b/core/src/form-link.ts @@ -11,6 +11,8 @@ interface UrlLike { declare const URL: { new (url: string, base?: string): UrlLike }; export const MAX_FORM_URLS = 20; +// Matches the capture URL cap (shell/capture MAX_CAPTURE_URL); core cannot import it. +export const MAX_FORM_URL = 8_000; // Hosts whose application form lives one path segment below the posting. const TRAILING_SEGMENTS: Readonly> = { @@ -24,6 +26,7 @@ export function formUrlKey( url: string, relCanonical?: string, ): string | undefined { + if (url.length > MAX_FORM_URL) return undefined; try { const parsed = new URL(url.trim()); if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') { diff --git a/shell/ui/Popup.tsx b/shell/ui/Popup.tsx index c5f56d1..9cdbfea 100644 --- a/shell/ui/Popup.tsx +++ b/shell/ui/Popup.tsx @@ -155,7 +155,12 @@ export function Popup(): VNode { const [recordNotice, setRecordNotice] = useState( undefined, ); + // Duplicate and repost facts for the application just made from this form. + const [newFacts, setNewFacts] = useState< + { match: Facts['match']; repost: string | undefined } | undefined + >(undefined); const linkingRef = useRef(false); + const keepHeldRef = useRef(false); const linkedIdRef = useRef(undefined); useEffect(() => { @@ -165,11 +170,15 @@ export function Popup(): VNode { // A match linked the form without the owner's click: drop the stale held // fill rather than let a later Choose… record it. The owner's own link // path records and clears held itself, and is excluded while in flight. + // Exception: onFilled holds a result on two recoverable paths (application + // gone, write rejected) where the popup still offers to record it; those + // keep it until the owner's next link (runLink resets this). useEffect(() => { if ( linkedIdRef.current !== undefined && heldRef.current !== undefined && - !linkingRef.current + !linkingRef.current && + !keepHeldRef.current ) { heldRef.current = undefined; setHeld(undefined); @@ -289,6 +298,8 @@ export function Popup(): VNode { id: Id; name: string; record?: FillRecord; + match?: Facts['match']; + repost?: string; }, ): Promise => { if (linkingRef.current) return Promise.resolve(false); @@ -300,6 +311,8 @@ export function Popup(): VNode { name: string; record?: FillRecord; held?: FillResult; + match?: Facts['match']; + repost?: string; } = { name: '' }; return app .apply((d) => { @@ -309,21 +322,31 @@ export function Popup(): VNode { got.id = built.id; got.name = built.name; got.record = built.record; + got.match = built.match; + got.repost = built.repost; if (built.record?.missing) throw new Error(MISSING); return built.ops; }) .then( () => { linkingRef.current = false; + // Only a successful owner link ends the keep; a failed one leaves + // the held result offered for a retry. + keepHeldRef.current = false; setChosenId(got.id); setLinking(false); + setRecordNotice(undefined); + setNewFacts( + got.match !== undefined || got.repost !== undefined + ? { match: got.match, repost: got.repost } + : undefined, + ); if (got.record !== undefined) { // Clear only the result this link recorded; a Fill that finished // during the write has replaced it and must survive. if (got.held !== undefined && heldRef.current === got.held) { setHeld(undefined); } - setRecordNotice(undefined); setRecordLine( got.record.recorded > 0 ? { @@ -367,6 +390,7 @@ export function Popup(): VNode { // Read when the fill completes, not at the click (FillSection is fixed). const id = linkedIdRef.current; if (id === undefined) { + keepHeldRef.current = false; setHeld(result); return; } @@ -383,12 +407,14 @@ export function Popup(): VNode { const record = got.record; if (record === undefined) return; if (record.missing) { + keepHeldRef.current = true; heldRef.current = result; setHeld(result); setChosenId(undefined); setRecordNotice(MISSING); return; } + keepHeldRef.current = false; setHeld(undefined); if (record.recorded > 0) { setRecordLine({ @@ -398,7 +424,12 @@ export function Popup(): VNode { }); } }, - (err: unknown) => setLinkError(messageOf(err)), + (err: unknown) => { + keepHeldRef.current = true; + heldRef.current = result; + setHeld(result); + setLinkError(messageOf(err)); + }, ); }; const linkNew = (names: { @@ -425,6 +456,8 @@ export function Popup(): VNode { id: built.applicationId, name: displayName(names.companyName, names.title), record: built.record, + match: built.match, + repost: built.repost, }; }); @@ -604,6 +637,17 @@ export function Popup(): VNode { recordLine={recordLine} /> ) : null} + {newFacts?.match === 'already_applied' ? ( + Already applied here. + ) : null} + {newFacts?.match === 'possible_match' ? ( + + Possible match: an application for the same company and title. + + ) : null} + {newFacts?.repost ? ( + {newFacts.repost} + ) : null} {fillStatus === 'ready' && linkedId !== undefined ? (