Skip to content

Eliminate code duplication in test setup and service data fetching#9

Merged
CropWatchDevelopment merged 5 commits into
masterfrom
copilot/refactor-similar-code-in-air
Jan 20, 2026
Merged

Eliminate code duplication in test setup and service data fetching#9
CropWatchDevelopment merged 5 commits into
masterfrom
copilot/refactor-similar-code-in-air

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 20, 2026

Qlty identified ~200 lines of duplicated code across test files and service classes in the air, water, soil, and traffic modules.

Changes

Test Setup Consolidation

  • Created createTestingModuleWithCommonProviders() in src/common/test-helpers.ts
  • Refactored 8 test files to use shared setup instead of duplicating 20+ lines each

Service Layer Abstraction

  • Created BaseDataService<T> abstract class in src/common/base-data.service.ts
  • Encapsulates common findOne() implementation: Supabase query, timezone validation/formatting, error handling
  • Refactored AirService, WaterService, SoilService, TrafficService to extend base class

Before:

export class AirService {
  async findOne(devEui: string, startDate: Date, endDate: Date, timezone?: string) {
    const normalizedTimeZone = timezone?.trim() || null;
    if (normalizedTimeZone) {
      this.timezoneFormatter.assertValidTimeZone(normalizedTimeZone);
    }
    const { data, error } = await this.supabaseService
      .getClient()
      .from('cw_air_data')
      // ... 40+ more duplicated lines
  }
}

After:

export class AirService extends BaseDataService<'cw_air_data'> {
  constructor(supabaseService: SupabaseService, timezoneFormatter: TimezoneFormatterService) {
    super(supabaseService, timezoneFormatter, 'cw_air_data');
  }
}

Pattern applies identically to water, soil, and traffic services.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • scarf.sh
    • Triggering command: /usr/local/bin/node node ./report.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Qlty: Found 29 lines of similar code in 4 locations (mass = 159)</issue_title>
<issue_description>Issue on Qlty

Found 27 lines of similar code in 4 locations (mass = 83)
src/air/air.controller.spec.ts

import
{ Test, TestingModule }
from

'@nestjs/testing'
;
import
{ AirController }
from

'./air.controller'
;
import
{ AirService }
from

'./air.service'
;
import
{ SupabaseService }
from

'../supabase/supabase.service'
;
import
{ TimezoneFormatterService }
from

'../common/timezone-formatter.service'
;
describe(
'AirController'
,
() =>
{

let
controller: AirController;
beforeEach(
async
() => {
Found 25 lines of similar code in 4 locations (mass = 79)
src/air/air.service.spec.ts

import
{ Test, TestingModule }
from

'@nestjs/testing'
;
import
{ AirService }
from

'./air.service'
;
import
{ SupabaseService }
from

'../supabase/supabase.service'
;
import
{ TimezoneFormatterService }
from

'../common/timezone-formatter.service'
;
describe(
'AirService'
,
() =>
{

let
service: AirService;
beforeEach(
async
() => {

const

module
: TestingModule =
await
Test.createTestingModule({
Found 29 lines of similar code in 4 locations (mass = 159)
src/air/air.service.ts

devEui: 

string
,

startDate
:
Date
,

endDate
:
Date
,
timezone?:
string
,
):
Promise
<TableRow<
'cw_air_data'

[]> {

const
normalizedTimeZone = timezone?.trim() ||
null
;

if
(normalizedTimeZone) {

this
.timezoneFormatter.assertValidTimeZone(normalizedTimeZone);
}
Found 19 lines of similar code in 2 locations (mass = 71)
src/power/power.service.spec.ts

import
{ Test, TestingModule }
from

'@nestjs/testing'
;
import
{ PowerService }
from

'./power.service'
;
describe(
'PowerService'
,
() =>
{

let
service: PowerService;
beforeEach(
async
() => {

const

module
: TestingModule =
await
Test.createTestingModule({

providers
: [PowerService],
}).compile();
Found 19 lines of similar code in 2 locations (mass = 71)
src/realtime/realtime.service.spec.ts

import
{ Test, TestingModule }
from

'@nestjs/testing'
;
import
{ RealtimeService }
from

'./realtime.service'
;
describe(
'RealtimeService'
,
() =>
{

let
service: RealtimeService;
beforeEach(
async
() => {

const

module
: TestingModule =
await
Test.createTestingModule({

providers
: [RealtimeService],
}).compile();
Found 27 lines of similar code in 4 locations (mass = 83)
src/soil/soil.controller.spec.ts

import
{ Test, TestingModule }
from

'@nestjs/testing'
;
import
{ SoilController }
from

'./soil.controller'
;
import
{ SoilService }
from

'./soil.service'
;
import
{ SupabaseService }
from

'../supabase/supabase.service'
;
import
{ TimezoneFormatterService }
from

'../common/timezone-formatter.service'
;
describe(
'SoilController'
,
() =>
{

let
controller: SoilController;
beforeEach(
async
() => {
Found 90 lines of similar code in 3 locations (mass = 423)
src/soil/soil.controller.ts

import
{ BadRequestException, Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards }
from

'@nestjs/common'
;
import
{ SoilService }
from

'./soil.service'
;
import
{ SoilDataDto }
from

'./dto/soil-data.dto'
;
import

type
{ CreateSoilDto }
from

'./dto/create-soil.dto'
;
import

type
{ UpdateSoilDto }
from

'./dto/update-soil.dto'
;
import
{ JwtAuthGuard }
from

'../auth/guards/jwt.auth.guard'
;
import
{ ApiBadRequestResponse, ApiBearerAuth, ApiInternalServerErrorResponse, ApiOkResponse, ApiParam, ApiQuery, ApiSecurity, ApiUnauthorizedResponse }
from

'@nestjs/swagger'
;
import
{ ErrorResponseDto }
from

'../common/dto/error-response.dto'
;
@controller
(
'soil'
)
Found 25 lines of similar code in 4 locations (mass = 79)
src/soil/soil.service.spec.ts

import
{ Test, TestingModule }
from

'@nestjs/testing'
;
import
{ SoilService }
from

'./soil.service'
;
import
{ SupabaseService }
from

'../supabase/supabase.service'
;
import
{ TimezoneFormatterService...


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jan 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
api Ready Ready Preview, Comment Jan 20, 2026 4:59am

Request Review

Co-authored-by: CropWatchDevelopment <111664979+CropWatchDevelopment@users.noreply.github.com>
Co-authored-by: CropWatchDevelopment <111664979+CropWatchDevelopment@users.noreply.github.com>
Co-authored-by: CropWatchDevelopment <111664979+CropWatchDevelopment@users.noreply.github.com>
Co-authored-by: CropWatchDevelopment <111664979+CropWatchDevelopment@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor similar code in AirController and AirService tests Eliminate code duplication in test setup and service data fetching Jan 20, 2026
@CropWatchDevelopment CropWatchDevelopment marked this pull request as ready for review January 20, 2026 05:08
@CropWatchDevelopment CropWatchDevelopment merged commit 44e7540 into master Jan 20, 2026
2 checks passed
@CropWatchDevelopment CropWatchDevelopment deleted the copilot/refactor-similar-code-in-air branch January 20, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qlty: Found 29 lines of similar code in 4 locations (mass = 159)

2 participants